简体   繁体   English

PDO的rowCount()不适用于PHP 5.2.6+

[英]PDO's rowCount() Not Working on PHP 5.2.6+

So I've been using PHP's PDO as my database goto class for a while now, unfortunately today after debugging for a while on a client's server (with PHP 5.2.6 installed) I discover this . 所以我一直在使用PHP的PDO作为我的数据库goto类,不幸的是今天在客户端服务器上调试了一段时间后(安装了PHP 5.2.6)我发现了这一点 We tried upgrading to the newest stable release (5.2.9) but the problem persists. 我们尝试升级到最新的稳定版本(5.2.9),但问题仍然存在。

Has anyone found a workaround? 有没有人找到解决方法?

The only way that databases can give you a count for the number of rows is by running the query and counting the number of rows. 数据库可以计算行数的唯一方法是运行查询并计算行数。

The mysql extension uses a buffered query mode by default that causes the entire dataset to be fetched into memory before control is returned to PHP and it can start to process the rows. 默认情况下,mysql扩展使用缓冲查询模式,导致整个数据集在控制返回到PHP之前被提取到内存中,并且可以开始处理行。

PDO uses an unbuffered mode by default which leads to lower latency in the page load time and is generally what you want. 默认情况下,PDO使用无缓冲模式,这会导致页面加载时间延迟降低,这通常是您想要的。 The trade off is that rowCount() won't return valid information until the entire dataset has been fetched. 权衡是rowCount()在获取整个数据集之前不会返回有效信息。

So how do you get that count? 那你怎么算这个数呢?

Easy: 简单:

$q = $db->query("SELECT ...");
$rows = $q->fetchAll();
$rowCount = count($rows);
echo "There are $rowCount rows\n";
foreach ($rows as $row) {
    print_r($row);
}

But that sucks because it queries all the rows up front and makes my page load slower, the old mysql extension didn't have this problem!? 但这很糟糕,因为它查询前面的所有行并使我的页面加载速度变慢,旧的mysql扩展没有这个问题!?

But that's exactly what the old mysql extension is actually doing under the covers; 但这正是旧的mysql扩展实际上正在做的事情; it's the only way to get that count. 这是实现这一目标的唯一途径。

You could do it through MySQL itself by using the FOUND_ROWS() function , not sure if there are any better alternatives. 您可以通过使用FOUND_ROWS()函数通过MySQL本身来完成 ,不确定是否有更好的替代方案。

Edit: It seems as though the only reason this was possible with MySQL is because it internally fetched all the result rows and buffered them, to be able to give you this information. 编辑:似乎这是MySQL可能的唯一原因是因为它在内部获取所有结果行并缓冲它们,以便能够为您提供此信息。 See mysql_unbuffered_query() . 请参阅mysql_unbuffered_query() If you use that function instead of mysql_query() , the mysql_num_rows() function will not work. 如果使用该函数而不是mysql_query() ,则mysql_num_rows()函数将不起作用。 If you really need to know the number of rows while using PDO, you can fetch all of the rows from PDO into an array and then use count() . 如果在使用PDO时确实需要知道行数,可以将PDO中的所有行提取到数组中,然后使用count()

Notice you should not use rowCount() for SELECT queries since PDOStatement->rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object. 请注意,不应将rowCount()用于SELECT查询,因为PDOStatement-> rowCount()返回受相应PDOStatement对象执行的最后一个DELETE,INSERT或UPDATE语句影响的行数。

Instead you might use fetchAll() into an array and then count(). 相反,您可以将fetchAll()用于数组,然后使用count()。

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

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