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

PDO#39;s rowCount() Not Working on PHP 5.2.6+(PDO 的 rowCount() 不适用于 PHP 5.2.6+)
本文介绍了PDO 的 rowCount() 不适用于 PHP 5.2.6+的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

所以我使用 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+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)