What is the best way to accomplish this?
I have a PHP function that outputs a MySQL while loop. It works fine, displays every row as expected. However, I need to not echo the results, but rather put them as a variable to work with preexisting code. Changing that obviously limits it to only one result currently.
I assume it's got to be inserted as an array of sorts. Thinking about looping a loop is making my head hurt haha.
Any help would be much appreciated.
Just do this,
$result_rows = array();
while ($row = mysql_fetch_assoc($result)) {
$result_rows[] = $row; //or $row['fieldname'] if you're looking for something specific
}
Note: MYSQL extensions are deprecated, use MySQLi or PDO_MySQL if possible.
You could use something like this
$resultsArray = Array();
$statement = $db->query("SELECT * FROM table");
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
$resultsArray [] = array(
'field1' => $row["column1"],
'field2' => $row["column2"],
'field3' => $row["column3"]);
}