mysql_select_db($my_db, $con);
$result = mysql_query("SELECT * FROM word_test");
$i=1;
while($row = mysql_fetch_array($result))
{
$a = array(
$i => array(
0 => $row['question'],
1 => $row['op_1'],
2 => $row['op_2'],
3 => $row['op_3'],
4 => $row['op_4'],
6 => $row['ans']
),
);
$i=$i+1;
}//while loop ended
print_r($a);
-
i'm getting only value in last arrayuser1657890– user16578902012-09-09 09:24:32 +00:00Commented Sep 9, 2012 at 9:24
-
print_r is outside the while loopgrasshopper– grasshopper2012-09-09 09:31:03 +00:00Commented Sep 9, 2012 at 9:31
-
1use PDO instead, mysql_* is being deprecatedgrasshopper– grasshopper2012-09-09 09:34:02 +00:00Commented Sep 9, 2012 at 9:34
Add a comment
|
1 Answer
You're overwriting the array in $a with each iteration of the loop. Pull the definition of $a outside the loop, and use array_push to append each row:
$a = array();
while($row = mysql_fetch_array($result))
{
$i => array(
0 => $row['question'],
1 => $row['op_1'],
2 => $row['op_2'],
3 => $row['op_3'],
4 => $row['op_4'],
6 => $row['ans']
);
array_push($a, $i);
$i=$i+1;
}