Skip to content
Advertisement

How to print multiple value column in PHP

I have a SQL Server database about Customer information and CustomerPhone has a multiple values column which is C_Phone.

This is my code:

JavaScript

This is the result I got. How can I print the second phone number under the first one? enter image description here

If I run the SQL query above on the SSMS, I got this result with Customer.C_Code = 7:

enter image description here

Advertisement

Answer

The reason for this unexpected behaviour is that you need to echo the customer phone from the first fetched row (echo "<p class="card-text">C_Phone: ". $row['C_Phone'] . "<br>"; instead of echo "<p class="card-text">C_Phone: ";).

You may also consider the following:

  • Use explicit JOIN syntax.
  • Use parameterized queries. As is mentioned in the documentation, the sqlsrv_query function is well-suited for one-time queries and should be the default choice to execute queries unless special circumstances apply and sqlsrv_query function does both statement preparation and statement execution, and can be used to execute parameterized queries.

The following example, based on your code, is a possible solution to your problem:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement