从Select语句中获取行数

时间:2021-01-19 01:54:07

I have this:

我有这个:

$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?

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

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

Thanks all

谢谢大家

2 个解决方案

#1


8  

SELECT count(*) FROM this_table is an option...

SELECT count(*)FROM this_table是一个选项......

Regarding rowCount:

关于rowCount:

PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.

PDOStatement :: rowCount()返回由相应PDOStatement对象执行的最后一个DELETE,INSERT或UPDATE语句影响的行数。

If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. **

如果关联的PDOStatement执行的最后一个SQL语句是SELECT语句,则某些数据库可能会返回该语句返回的行数。 **

However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.

但是,并不是所有数据库都能保证这种行为,不应依赖于便携式应用程序。

#2


5  

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.

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

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

From another question Chad provided this insight:

另一个问题是,查德提供了这样的见解:

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().

似乎这是MySQL可能实现的唯一原因是因为它在内部获取了所有结果行并对其进行了缓冲,以便能够为您提供此信息。请参阅mysql_unbuffered_query()。如果使用该函数而不是mysql_query(),则mysql_num_rows()函数将不起作用。如果在使用PDO时确实需要知道行数,可以将PDO中的所有行提取到数组中,然后使用count()。

Hope this is useful to someone.

希望这对某人有用。

#1


8  

SELECT count(*) FROM this_table is an option...

SELECT count(*)FROM this_table是一个选项......

Regarding rowCount:

关于rowCount:

PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.

PDOStatement :: rowCount()返回由相应PDOStatement对象执行的最后一个DELETE,INSERT或UPDATE语句影响的行数。

If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. **

如果关联的PDOStatement执行的最后一个SQL语句是SELECT语句,则某些数据库可能会返回该语句返回的行数。 **

However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.

但是,并不是所有数据库都能保证这种行为,不应依赖于便携式应用程序。

#2


5  

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.

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

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

From another question Chad provided this insight:

另一个问题是,查德提供了这样的见解:

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().

似乎这是MySQL可能实现的唯一原因是因为它在内部获取了所有结果行并对其进行了缓冲,以便能够为您提供此信息。请参阅mysql_unbuffered_query()。如果使用该函数而不是mysql_query(),则mysql_num_rows()函数将不起作用。如果在使用PDO时确实需要知道行数,可以将PDO中的所有行提取到数组中,然后使用count()。

Hope this is useful to someone.

希望这对某人有用。