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

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

问题描述

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 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天全站免登陆