PHP/MySQL: How to get multiple values from a PHP database method

methodsMySQLoopPHP

Sorry for the incredibly newbie question, but I can see myself drifting into bad practices if I don't ask.

I have a PHP method that I want to return all the values of a given database column, in order to place the contents in a dropdown menu for a HTML form. I could obviously construct the whole HTML in the PHP method and return that as a string, but I imagine this is pretty bad practice.

Since PHP methods can only return one value, I imagine I'll need to call the method several times to populate the dropdown menu, or pass an array from the method.

What would be a good solution to this (presumably) common problem? Thanks.

Best Answer

Well, an array is one value, containing tons of other values. So just have your method return a array of results.

edit: as Hammerstein points out you could use objects but its as good/bad as arrays depending on context. Very similar.