从 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 语句返回的行数?
谢谢大家
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?
Thanks all
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
SELECT count(*) FROM this_table
是一个选项...关于 rowCount:
PDOStatement::rowCount() 返回受相应 PDOStatement 执行的最后一个 DELETE、INSERT 或 UPDATE 语句影响的行数目的。
如果关联的 PDOStatement 执行的最后一个 SQL 语句是 SELECT 语句,则某些数据库可能会返回该语句返回的行数。 **
但是,不能保证所有数据库都具有此行为,并且不应依赖于可移植应用程序。
SELECT count(*) FROM this_table
is an option...Regarding rowCount:
PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.
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. **
However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.
我找到了一个解决方案,使用 fetchAll 然后在这个数组上使用 count - 这就是 MySQL 内部所做的,有点低效,但它对我有用。
在另一个问题 Chad 中提供了以下见解:
希望这对某人有用。
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.
From another question Chad provided this insight:
Hope this is useful to someone.
您可以在此处使用 mysqli_num_rows() 函数。它返回 sql 查询的行数。
You can use mysqli_num_rows() function here. It returns number of rows from the sql query.