且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从Select语句中获取行数

更新时间:2023-02-06 07:53:14

我找到一个解决方案,使用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().

希望这对某人有用。