且构网

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

相当于 mysql_num_rows 或 mssql_num_rows 的 PDO

更新时间:2023-02-26 10:50:48

如果你想计算行数,你可以使用 PDO:

If you want to count the rows you can do this with PDO:

$sql = 'select * from users';
$data = $conn->query($sql);
$rows = $data->fetchAll();
$num_rows = count($rows);

文档.

There is no way to directly count rows when using a SELECT statement with PDO as stated in the docs.

PDOStatement::rowCount() 返回受相应的最后一个 DELETEINSERTUPDATE 语句影响的行数PDOStatement 对象.

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

仅在绝对需要计数时才进行行计数,否则您可以验证查询是否与其他方法一起使用.如果您希望从表中返回数千行,您也不应该使用此方法,而是在查询中使用 COUNT() 函数来执行计数.

Only do a row count if you absolutely need the count, otherwise you can verify that the query worked with other methods. You should also not use this method if you expect to be returning thousands of rows from a table, instead, use the COUNT() function in a query for just performing the count.