-5

The problem i'm having is from the 4th line of code listed below. I get an error that says

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, string given

I don't have the variable enclosed in " " or ' ' so I'm not sure where the string recognition is coming from at this point. Can tell me how to fix this error?

$query = "SELECT * FROM questions WHERE id='question' LIMIT 5";
$result = mysqli_query($connection, $query);
if($query === FALSE) { die(mysql_error()); } 

while($row = mysqli_fetch_array($query)){
    $id = $row['id'];
    $thisQuestion = $row['question'];
    $question_id = $row['question_id'];
    $q = '<h2>'.$thisQuestion.'</h2>';
    $query2 = "SELECT * FROM answers WHERE question_id='$question' ORDER BY rand() LIMIT 5";
    while($row2 = mysqli_fetch_array($query2)){
        //...
    }
}
kero
  • 10,647
  • 5
  • 41
  • 51
emayne_designer
  • 31
  • 1
  • 1
  • 6
  • I would recommend taking closer looks at your code when you get error messages such as these. When we are first starting to use a new language, we all tend to make careless mistakes such as referencing the wrong variable as you have done here. – Brandon White May 04 '14 at 18:46
  • thank you. i was so intent on syntax errors that i didn't realize i was trying to pass the wrong variable. – emayne_designer May 04 '14 at 19:15

1 Answers1

7

You have:

 mysqli_fetch_array($query)

Should be:

mysqli_fetch_array($result) 

Also in line 3 you have:

if($query === FALSE) { die(mysql_error()); } 

Should be rather:

if ($result === FALSE) { die(mysql_error()); } 
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • 1
    better `mysqli_fetch_assoc`. – Lkopo May 04 '14 at 19:01
  • thank you! that worked for that errror. – emayne_designer May 04 '14 at 19:13
  • You have an error. [`mysql_error()`](https://www.php.net/manual/en/function.mysql-error.php) worked only for the old API. Please consider switching error mode on instead. [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Oct 31 '19 at 23:11