Second answer in response to your edit:
$result = mysql_query($sql); $num = mysql_num_rows($result); $i = 0; $id = array(); $name = array(); $value = array(); if ($num > 0) { while ($row = mysql_fetch_assoc($result)) { $id[$i] = $row['id']; $name[$i] = $row['name']; $value[$i] = $row['value']; $i++; } }
This will cover your result using the $i counter as the key for your resulting arrays.
EDIT
Additional answer in response to your comment:
while ($row = mysql_fetch_assoc($result)) { foreach($row as $column_name => $column_value) { $temp_array[$column_name][$i] = $column_value; } $i++; } foreach ($temp_array as $name => $answer) { $$name = $answer; }
This code creates a temporary multidimensional array for storing column names and loop values around this array to create variable array variables. As a side, I should not have used the temp array, since $$column_name[$i] does not work, I would like to see alternative answers to this problem.
Final note @ Paisal, I see you never accepted the answer, I wouldn’t have put in much effort if I had seen this before!
source share