沃梦达 / 编程问答 / php问题 / 正文

从 Select 语句中获取行数

Get Number of Rows from a Select statement(从 Select 语句中获取行数)

本文介绍了从 Select 语句中获取行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个:

$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=$mdbFilename", $username, $password);

$sql = "SELECT * FROM this_table";

$stmt = $dbh->query($sql);

//num of rows?

如何获取从该 SELECT 语句返回的行数?

How do I get the number of rows returned from that SELECT statement?

谢谢大家

推荐答案

我找到了一个解决方案,使用 fetchAll 然后在这个数组上使用 count - 这就是 MySQL 在内部所做的,有点低效,但对我有用.

I have found a solution, using fetchAll and then using count on this array - which is what MySQL does anyway internally, a bit inefficient but it works for me.

$q = $db->query("SELECT ...");
$rows = $q->fetchAll();
$rowCount = count($rows);

来自另一个问题 Chad 提供了这一见解:

From another question Chad provided this insight:

似乎是唯一的原因这对 MySQL 是可能的因为它在内部获取了所有结果行并缓冲它们,成为能够为您提供这些信息.看mysql_unbuffered_query().如果你使用该功能而不是mysql_query(), mysql_num_rows()功能将不起作用.如果你真的需要知道行数,而使用 PDO,您可以获取所有行从 PDO 到一个数组,然后使用计数().

It seems as though the only reason this was possible with MySQL is because it internally fetched all the result rows and buffered them, to be able to give you this information. See mysql_unbuffered_query(). If you use that function instead of mysql_query(), the mysql_num_rows() function will not work. If you really need to know the number of rows while using PDO, you can fetch all of the rows from PDO into an array and then use count().

希望这对某人有用.

这篇关于从 Select 语句中获取行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:从 Select 语句中获取行数

基础教程推荐