Jeff Gu Kang
Jeff Gu Kang

Reputation: 4889

How can I store results from MySQL to PHP array?

I tried storing data to php array from mysql using under code. But it's not working. This code:

echo $answerArray[count][i];

shows the correct result. But this code:

echo $answerArray[0][0];

doesn´t show anything.

What should I do to fix it? Thank you.

Full code:

   $count = 0; //answer count
   $answerArray = array();
    while ($row = mysqli_fetch_array($resultFromR, MYSQLI_ASSOC)) { //add array from db
     for($i = 0; $i < $questionNumber; $i++) {
     $j = $i + 1;
     $answerArray[count][i] = $row["num$j"];
     echo $answerArray[count][i]; //is working.
     }
$count++;
}
echo '<br />';
echo $answerArray[0][0]; //something wrong!!! I cannot get anything from this.

Upvotes: 3

Views: 217

Answers (2)

Alon Eitan
Alon Eitan

Reputation: 12025

maybe this will work

 $answerArray[$count][$i] = $row["num$j"];

(add $ before count and i)

Upvotes: 2

Rohit Chopra
Rohit Chopra

Reputation: 2821

Not sure but shouldn't the [i] be [$i]?

Upvotes: 1

Related Questions