I have my own module which uses db_result() and I get a PHP warning about $result variable in that function (mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given).
All the things inside the post.

I have a function which loads info about the dealer, the person who can invite new users to earn the money.
Here is the code:

<?php
function tg_dealer_load_dealer($uid = NULL) {
  
  static $dealers = array();
  
  if (!$uid) {
    global $user;
    $uid = $user->uid;
  }
  
  if (!empty($dealers[$uid])) {
    return $dealers[$uid];
  }
  
  $dealer = db_fetch_object(db_query("SELECT * FROM {tg_dealer} WHERE uid = %d", $uid));
  $dealer->invited = db_result(db_query("SELECT COUNT(*) FROM {tg_dealer_invited} WHERE dealer_uid = %d", $uid));
  $dealer->ref_clicks = db_result(db_query("SELECT COUNT(*) FROM {tg_dealer_ref_log} WHERE dealer_uid = %d", $uid));
  
  $dealers[$uid] = $dealer;
  return $dealer? drupal_unpack($dealer) : FALSE;
}
?>

Every time the function is called, I get a PHP warning "mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given" in db_result() function of database.mysqli.inc
So I converted my function to this:

<?php
function tg_dealer_load_dealer($uid = NULL) {
  ...
  
  $dealer = db_fetch_object(db_query("SELECT * FROM {tg_dealer} WHERE uid = %d", $uid));
  $result = db_query("SELECT COUNT(*) FROM {tg_dealer_invited} WHERE dealer_uid = %d", $uid);
  dsm($result);
  $dealer->invited = db_result($result);
  dsm($result);
  dsm($dealer->invited);
  $result = db_query("SELECT COUNT(*) FROM {tg_dealer_ref_log} WHERE dealer_uid = %d", $uid);
  dsm($result);
  $dealer->ref_clicks = db_result($result);
  dsm($result);
  
  $dealers[$uid] = $dealer;
  return $dealer? drupal_unpack($dealer) : FALSE;
}
?>

And I added the line to db_result() function. I use var_dump() here because dsm() is not yet declared at this early point, and drupal_set_message() expects a string parameter.

<?php
function db_result($result) {
  if (!is_object($result))  var_dump($result);
  if ($result && mysqli_num_rows($result) > 0) {
    // The mysqli_fetch_row function has an optional second parameter $row
    // but that can't be used for compatibility with Oracle, DB2, etc.
    $array = mysqli_fetch_row($result);
    return $array[0];
  }
  return FALSE;
}

?>

Both the first dsm() in my function show me the $result is mysqli_result object.
But var_dump() between them generates "bool(true)".
One more strange thing: it doesn't happen with my next db_result() call.
And one more strange thing: the $dealer object is populated properly, so the number of invited people is right. And it is just impossible because of if() statement which shouldn't be TRUE because the second part of the statement must be FALSE.

PHP Version 5.4.9. Drupal 6, the latest.
Any help appreciated.

Comments

Jaypan’s picture

You didn't tell us which line of code is forcing the error. But usually these are a result of an SQL syntax error. Are you sure you have your table and column names correct?

Victor Safronov’s picture

This line is forcing an error:

<?php
$dealer->invited = db_result(db_query("SELECT COUNT(*) FROM {tg_dealer_invited} WHERE dealer_uid = %d", $uid));
?>

Of course I know the SQL is fine, the table is exist and so on. Even more, $dealer->invited gets the right integer value after that, but with this unexplainable warning.

Jaypan’s picture

$dealer->invited gets the right integer value after that

If that is the case, then I believe you are misdiagnosing the location where the error is occurring. If the error you are reporting was coming from this line of code, then $dealer->invited would be NULL or FALSE.

Victor Safronov’s picture

That's why I call this situation unexplainable.
When I comment out this line, the warning disappears.

Victor Safronov’s picture

You were right, the explanation is found. I'm stupid, kill me somebody.

I had the code which shows the invited users if $dealer->invited > 0.
This code used pager_query(), where I by mistake used '' instead of NULL for $count_query parameter, so real trouble was in db_result() which is called inside of pager_query().
That is why I was confused about the warning, which disappear, when I comment the db_result() call.