简体   繁体   English

PHP MySQLi num_rows 总是返回 0

[英]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.我构建了一个 class,它利用了 PHP 内置的 MySQLi class 的功能,旨在简化数据库交互。 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.但是,使用 OOP 方法,我很难让 num_rows 实例变量在运行查询后返回正确的行数。 Take a look at a snapshot of my class...看看我的 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".结果 object 中的其他值是准确的,例如“field_count”。 Any help is greatly appreciated.任何帮助是极大的赞赏。

Thank you for your time.感谢您的时间。

Possible Bug: http://www.php.net/manual/en/mysqli-result.num-rows.php#104630可能的错误: http://www.php.net/manual/en/mysqli-result.num-rows.php#104630

Code is from source above (Johan Abildskov):代码来自上面的源代码(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);

This could be normal behavior when you disable buffering of the result rows with MYSQLI_USE_RESULT当您使用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)

I had the same problem and found the solution was to put:我遇到了同样的问题,发现解决方案是:

$result->store_result();

..after the execution of the $query and before ..在 $query 执行之后和之前

echo $result->num_rows;回声 $result->num_rows;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM