Warning problem: expects parameter 1 to be mysqli_result

Possible duplicate:
mysql_fetch_array () expects parameter 1 to be a resource, boolean is set to select

The following warning is below, and I was wondering how to fix it.

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given on line 65 

The code is around this section of the PHP code below. If necessary, I can specify the full code.

 // function to retrieve average and votes function getRatingText(){ $dbc = mysqli_connect ("localhost", "root", "", "sitename"); $sql1 = "SELECT COUNT(*) FROM articles_grades WHERE users_articles_id = '$page'"; $result = mysqli_query($dbc,$sql1); $total_ratings = mysqli_fetch_array($result); $sql2 = "SELECT COUNT(*) FROM grades JOIN grades ON grades.id = articles_grades.grade_id WHERE articles_grades.users_articles_id = '$page'"; $result = mysqli_query($dbc,$sql2); $total_rating_points = mysqli_fetch_array($result); if (!empty($total_rating_points) && !empty($total_ratings)){ $avg = (round($total_rating_points / $total_ratings,1)); $votes = $total_ratings; echo $avg . "/10 (" . $votes . " votes cast)"; } else { echo '(no votes cast)'; } } 
+7
php mysql mysqli error-handling
source share
2 answers

mysqli_query() returns FALSE if there was an error in the query. Therefore you should check it out ...

 /* Select queries return a resultset */ if ($result = mysqli_query($dbc, "SELECT Name FROM City LIMIT 10")) { printf("Select returned %d rows.\n", $result->num_rows); /* free result set */ $result->close(); } 

See this link for the mysqli_query link http://php.net/manual/en/mysqli.query.php

+13
source share

The waterfall is probably right. Change the code as follows:

 $result = mysqli_query($dbc,$sql1) or die(mysqli_error($dbc)); // and $result = mysqli_query($dbc,$sql2) or die(mysqli_error($dbc)); 

PS: Just wondering what is the $page ? You forgot to do:

 global $page; 
+2
source share

All Articles