PHP PDO - 行数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2700621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:24:32  来源:igfitidea点击:

PHP PDO - Num Rows

phpmysqlpdo

提问by Ian

PDO apparently has no means to count the number of rows returned from a select query (mysqlihas the num_rowsvariable).

PDO 显然没有办法计算从选择查询返回的行数(mysqlinum_rows变量)。

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

有没有办法做到这一点,缺乏使用count($results->fetchAll())

采纳答案by Pascal MARTIN

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

根据手册,有一种PDOStatement->rowCount方法;但不应该使用(引用)

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

对于大多数数据库, PDOStatement::rowCount()不返回受SELECT语句影响的行数。
相反,用于 PDO::query()发出SELECT COUNT(*)与预期语句具有相同谓词的SELECT语句,然后用于 PDOStatement::fetchColumn()检索将返回的行数。
然后您的应用程序可以执行正确的操作。


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.


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

回答by Your Common Sense

Although PDO apparently has all means to count the number of rows returned from a select query for mysql, what is more important is that there is no usefor such a function in the first place.

虽然 PDO 显然有办法计算从 mysql 的 select 查询返回的行数,但更重要的是,首先没有使用这样的函数。

Every time you have an idea to use rowCount() for a SELECT query, it would be either superfluous or even harmful. See PDO rowCount():

每当您想到将 rowCount() 用于 SELECT 查询时,它要么是多余的,要么是有害的。见PDO rowCount()

  • in case you have the data selected already, this function is superfluous as you simply can count the data
  • in case you want to use this function to get the count only, it would harmful, as you should never select data only to count it. Select only count, using SELECT count(*) instead.
  • 如果您已经选择了数据,则此功能是多余的,因为您只需计算数据
  • 如果您只想使用此函数来获取计数,那将是有害的,因为您永远不应该只选择数据来计算它。只选择计数,使用 SELECT count(*) 代替。

回答by Angelstam

Another option that may be closer to the num_rowsvariable in mysqliand the corresponding C API function mysql_num_rows(). Is to use the MySQL function FOUND_ROWS()that returns the same information without having to count all the records in the result again.

另一个选项可能更接近num_rows变量 inmysqli和相应的 C API 函数mysql_num_rows()。就是使用mysql函数FOUND_ROWS()返回相同的信息,而不必再次统计结果中的所有记录。

Example:

例子:

$pdoDB->query('SELECT FOUND_ROWS()')->fetchColumn()

回答by The Naga Tanker

The Easiest

最简单的

  $stmt = $datalink->query('SELECT * FROM projects');
  $rows = $stmt->fetchAll();
  $num_rows = count($rows);
  echo $num_rows ;