PDO的rowCount()在PHP 5.2.6+上不起作用 [英] PDO's rowCount() Not Working on PHP 5.2.6+

查看:77
本文介绍了PDO的rowCount()在PHP 5.2.6+上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我已经使用PHP的PDO作为数据库goto类已有一段时间了,不幸的是,今天在客户端服务器(安装了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. 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.

默认情况下,mysql扩展使用缓冲查询模式,该模式导致将整个数据集提取到内存中,然后再将控制权返回给PHP,并且它可以开始处理行.

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使用无缓冲模式,这可以降低页面加载时间的延迟,这通常是您所需要的.需要权衡的是,在获取整个数据集之前,rowCount()不会返回有效信息.

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?

简单:

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

但是那很烂,因为它查询所有前面的行,并使我的页面加载速度变慢,旧的mysql扩展没有这个问题!?

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; it's the only way to get that count.

这篇关于PDO的rowCount()在PHP 5.2.6+上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆