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
方法;但不应使用(引用):
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屋!