PHP PDO - 行数 [英] PHP PDO - Num Rows
问题描述
PDO 显然无法计算从选择查询返回的行数(mysqli
有 num_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 aSELECT
statement.
Instead, usePDO::query()
to issue aSELECT COUNT(*)
statement with the same predicates as your intendedSELECT
statement, then usePDOStatement::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屋!