I have a fairly simple problem to which I've been unable to find a solution. I'd like to fill an array with the contents of a mySQL column.
Basically I have a loop that gathers the result set, and I need to take a specific column from this result set and put it into an array.
foreach ($results as $row){
$names = array();
$names[] = $row['name'];
};
Let's say there were 40 names in the result set they should now all be in the $names array, but I only get the last result when I attempt to print the contents on screen:
echo $names[0]; or print_r($names);
I have also tried:
while($row = mysql_fetch_array($results)) {
$names[] = $row['name'];
}
However I already have the foreach setup and working so I don't want to introduce another loop just to populate the array.
I know the query and loop are both working because I can echo out the value of each name directly by placing:
echo $row['name']
in the loop and see the resulting names print on screen.