Skip to content
Advertisement

Return all rows from an array

I’m trying to return a comma separated list of location names from a database.

So far I have the SQL statement that I know works because it returns the correct results when tested in phpMyAdmin.

I’m now trying to get the results returned and printed on screen in php.

The code I have bellow only seems to return the first result.

JavaScript

Advertisement

Answer

You need to remove the implode and return outside your while loop, otherwise your loop will stop after the first value:

JavaScript

Note that you can achieve this result directly in your query using GROUP_CONCAT:

JavaScript

Note that dependent on the source of your $id value, you may need to look into prepared statements to protect yourself from SQL injection.

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