PHP PDO - 行数 [英] PHP PDO - Num Rows

查看:23
本文介绍了PHP PDO - 行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PDO 显然无法计算从选择查询返回的行数(mysqlinum_rows 变量).

PDO apparently has no means to count the number of rows returned from a select query (mysqli has the num_rows variable).

除了使用 count($results->fetchAll()) 之外,有没有办法做到这一点?

Is there a way to do this, short of using count($results->fetchAll()) ?

推荐答案

根据手册,有一个PDOStatement->rowCount 方法;但不应该使用它(quoting) :

According to the manual, there is a PDOStatement->rowCount method ; but it shouldn't be used (quoting) :

对于大多数数据库,PDOStatement::rowCount() 没有返回受影响的行数SELECT 语句.
相反,使用PDO::query() 发出一个 SELECT COUNT(*) 语句谓词作为您的预期 SELECT声明,然后使用PDOStatement::fetchColumn() 到检索将要执行的行数被退回.
然后您的应用程序可以执行正确的操作.

For most databases, PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement.
Instead, use PDO::query() to issue a SELECT COUNT(*) statement with the same predicates as your intended SELECT statement, then use PDOStatement::fetchColumn() to retrieve the number of rows that will be returned.
Your application can then perform the correct action.


如果您已经有一个记录集,并且想知道其中有多少行,则必须使用 fetch* 方法之一来获取数据;并使用计数——就像你建议的那样.


If you already have a recordset, and want to know how many lines are in it, you'll have to fetch the data, using one of the fetch* methods ; and use count -- like you suggested.

这篇关于PHP PDO - 行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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