从Select语句中获取行数 [英] Get Number of Rows from a Select statement

查看:217
本文介绍了从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获取所有的
行到数组,然后
使用count()。 / p>

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语句中获取行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆