所以我使用 PHP 的 PDO 作为我的数据库 goto 类已经有一段时间了,不幸的是今天在客户端服务器上调试了一段时间后(安装了 PHP 5.2.6)我发现 这个.我们尝试升级到最新的稳定版本 (5.2.9),但问题仍然存在.
有没有人找到解决方法?
数据库可以为您计算行数的唯一方法是运行查询并计算行数.
mysql 扩展默认使用缓冲查询模式,这会导致在控制权返回给 PHP 之前将整个数据集提取到内存中,并开始处理行.
PDO 默认使用无缓冲模式,这会降低页面加载时间的延迟,这通常是您想要的.权衡是 rowCount() 在获取整个数据集之前不会返回有效信息.
那么你是如何得到这个数量的?
简单:
$q = $db->query("SELECT ...");$rows = $q->fetchAll();$rowCount = count($rows);echo "有 $rowCount 行
";foreach ($rows 作为 $row) {打印_r($row);}
<块引用>
但这很糟糕,因为它会在前面查询所有行并使我的页面加载速度变慢,旧的 mysql 扩展没有这个问题!?
但这正是旧的 mysql 扩展在幕后实际做的事情;这是获得该计数的唯一方法.
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. We tried upgrading to the newest stable release (5.2.9) but the problem persists.
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.
PDO uses an unbuffered mode by default which leads to lower latency in the page load time and is generally what you want. The trade off is that rowCount() won't return valid information until the entire dataset has been fetched.
So how do you get that count?
Easy:
$q = $db->query("SELECT ...");
$rows = $q->fetchAll();
$rowCount = count($rows);
echo "There are $rowCount rows
";
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!?
But that's exactly what the old mysql extension is actually doing under the covers; it's the only way to get that count.
这篇关于PDO 的 rowCount() 不适用于 PHP 5.2.6+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!