热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

PHPMySQLinum_rows始终返回0-PHPMySQLinum_rowsAlwaysReturns0

IhavebuiltaclasswhichleveragestheabilitiesofPHPsbuilt-inMySQLiclass,anditisintende

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...

我已经构建了一个利用PHP内置MySQLi类功能的类,它旨在简化数据库交互。但是,使用OOP方法,在运行查询后,num_rows实例变量返回正确的行数时遇到困难。看看我班上的快照......

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.

怎么这不准确?结果对象中的其他值是准确的,例如“field_count”。任何帮助是极大的赞赏。

Thank you for your time.

感谢您的时间。

2 个解决方案

#1


5  

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:

也可以使用Procedural样式进行验证:

/* determine number of rows result set */
$row_cnt = mysqli_num_rows($result);

#2


1  

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)

推荐阅读
author-avatar
手机用户2502854251
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有