Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP MySQLi num_rows Always Returns 0

I have built a class which leverages the abilities of PHP's built-in MySQLi class, and it is intended to simplify database interaction. However, using an OOP approach, I am having a difficult time with the num_rows instance variable returning the correct number of rows after a query is run. Take a look at a snapshot of my class...

class Database {
//Connect to the database, all goes well ...

//Run a basic query on the database
  public function query($query) {
  //Run a query on the database an make sure is executed successfully
    try {
    //$this->connection->query uses MySQLi's built-in query method, not this one
      if ($result = $this->connection->query($query, MYSQLI_USE_RESULT)) {
        return $result;
      } else {
        $error = debug_backtrace();

        throw new Exception(/* A long error message is thrown here */);
      }
    } catch (Exception $e) {
      $this->connection->close();

      die($e->getMessage());
    }
  }

//More methods, nothing of interest ...
}

Here is a sample usage:

$db = new Database();
$result = $db->query("SELECT * FROM `pages`"); //Contains at least one entry
echo $result->num_rows; //Returns "0"
exit;

How come this is not accurate? Other values from result object are accurate, such as "field_count". Any help is greatly appreciated.

Thank you for your time.

like image 239
Oliver Spryn Avatar asked Jun 30 '11 17:06

Oliver Spryn


People also ask

What value does Mysqli_query return?

Return Values ¶ For successful queries which produce a result set, such as SELECT, SHOW, DESCRIBE or EXPLAIN , mysqli_query() will return a mysqli_result object. For other successful queries, mysqli_query() will return true .

What does Mysqli_num_rows return?

The mysqli_num_rows() function returns the number of rows in a result set.

What does MySQLi () do?

The MySQLi functions allows you to access MySQL database servers. Note: The MySQLi extension is designed to work with MySQL version 4.1. 13 or newer.

What is $row in PHP?

Return Value: Returns an array of strings that corresponds to the fetched row. NULL if there are no more rows in result set. PHP Version: 5+


3 Answers

Possible Bug: http://www.php.net/manual/en/mysqli-result.num-rows.php#104630

Code is from source above (Johan Abildskov):

$sql = "valid select statement that yields results"; 
if($result = mysqli-connection->query($sql, MYSQLI_USE_RESULT)) 
{ 
          echo $result->num_rows; //zero 
          while($row = $result->fetch_row()) 
        { 
          echo $result->num_rows; //incrementing by one each time 
        } 
          echo $result->num_rows; // Finally the total count 
}

Could also validate with the Procedural style:

/* determine number of rows result set */
$row_cnt = mysqli_num_rows($result);
like image 96
Phill Pafford Avatar answered Oct 09 '22 15:10

Phill Pafford


I had the same problem and found the solution was to put:

$result->store_result();

..after the execution of the $query and before

echo $result->num_rows;

like image 24
Alex Avatar answered Oct 09 '22 15:10

Alex


This could be normal behavior when you disable buffering of the result rows with MYSQLI_USE_RESULT

Disabling the buffer means that it`s up to you to fetch, store and COUNT the rows. You should use the default flag

$this->connection->query($query, MYSQLI_STORE_RESULT); 

Equivalent of

$this->connection->query($query)
like image 22
idragosalex Avatar answered Oct 09 '22 16:10

idragosalex