0

Can you please help me with this error?

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in on line 17

Here is the code,

$query="SELECT * FROM users WHERE id ='$adminID' AND role id = '2'";
$result = mysqli_query($db_connection, $query);
$num=mysqli_num_rows($result);  <--line 17
  • 1
    `role id` isn't going to work in that query. – superphonic Mar 27 '14 at 22:41
  • possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select) – John Conde Mar 27 '14 at 22:41

3 Answers3

0

Your query probably has an error. Shouldn't it be "role_id" or something similar instead of "role id"? Notice the space.

More info

Community
  • 1
  • 1
Martin
  • 160
  • 7
  • thanks, i am also getting a similar error: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, string given in on line 92 $query = "SELECT * FROM users where user_id = '$sendtoid'"; $dbResult=mysqli_query($db_connection, $query); while ($rows = mysqli_fetch_array($query)): – user3469896 Mar 27 '14 at 22:44
  • @user3469896 Same thing. Check your query for errors. Or paste the line here so we can check it out. – Martin Mar 27 '14 at 22:45
  • @user3469896 The problem is you're using $query instead of $dbResult on mysqli_fetch_array. The line should read while ($rows = mysqli_fetch_array($dbResult)): – Martin Mar 27 '14 at 22:48
  • while ($dbRow=mysqli_fetch_array($dbResult)) { $id=$dbRow["id"]; $subject=$dbRow["subject"]; $message=$dbRow["message"]; $first_name=$dbRow["first_name"]; $sendtoid=$dbRow["sendtoid"]; if ($sendtoid > 0) { //if the id of the user the message was sent to is 0, it means it was an admin //so displays admin $query = "SELECT * FROM users where user_id = '$sendtoid'"; $dbResult=mysqli_query($db_connection, $query); while ($rows = mysqli_fetch_array($query)): $to = $rows ['first_name']; endwhile; – user3469896 Mar 27 '14 at 22:50
  • I have tried using your adjustment, but I'm still getting the same error – user3469896 Mar 27 '14 at 23:37
  • @user3469896 Hmm, what's $query like if you echo it? To get the final query. – Martin Mar 27 '14 at 23:40
  • query echos ok, and the function still does what its suppose to. it just display nasty error message above? – user3469896 Mar 28 '14 at 00:49
  • @user3469896 I mean paste it here so that we can have a look at it :) – Martin Mar 28 '14 at 00:50
  • while ($dbRow=mysqli_fetch_array($dbResult)) { $id=$dbRow["id"]; $sendtoid=$dbRow["sendtoid"]; $subject=$dbRow["subject"]; $message=$dbRow["message"]; $adminID=$dbRow["user_id"]; $msgread=$dbRow["msg_read"]; $query = "SELECT * FROM users WHERE id = '$adminID'"; $result = mysqli_query($db_connection, $query); while ($rows = mysqli_fetch_array($result, $query)): – user3469896 Mar 28 '14 at 01:11
  • @user3469896 You're making a similar mistake. That last line should read `while ($rows = mysqli_fetch_array($result)):` You only need to pass $result to mysqli_fetch_array, you don't need to pass your $query to it, as you already passed it to mysqli_query() – Martin Mar 28 '14 at 01:24
0

$result is almost certainly boolean false, because your query fails as Martin points out. In addition to fixing your query, check for error:

$query="SELECT * FROM users WHERE id ='$adminID' AND role id = '2'";
$result = mysqli_query($db_connection, $query);
if($result === false) {
    // handle error
} else {
    $num=mysqli_num_rows($result);
    // keep going as desired
}

See man page at http://us3.php.net/mysqli_query.
Return Values ¶

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

RobP
  • 9,144
  • 3
  • 20
  • 33
0

If your query is invalid, it returns a boolean value of false instead of a resource. Triple check your query and make sure it's correct. That should solve your error.

Shane Reeves
  • 218
  • 1
  • 7