PDO equivalent of mysql_num_rows or mssql_num_rows(相当于 mysql_num_rows 或 mssql_num_rows 的 PDO)
问题描述
我知道以前有人问过这个问题,但似乎解决方案是针对所提出的问题的.
I know this question has been asked before but it seems like the solutions have been specific to the problem presented.
我有一个包含数百个使用 mssql_num_rows 实例的代码库.
I have a codebase with hundreds of instances where mssql_num_rows is used.
代码示例:
$db->execute($sql);
if ($db->getRowsAffected() > 0) {
$total = $db->fetch();
在数据库类中:
$this->rowsaffected = mssql_num_rows($this->query_result);
- 我无法创建通用的
SELECT count(*) FROM table
查询,因为我有太多特定的选择语句. - 我可以运行
preg_replace
来删除SELECT 和 FROM
之间的所有内容,然后用COUNT(*)
替换并运行第二个查询但这假设所有查询都以某种方式设置. - 我可以先
fetchAll
然后count()
结果,但这意味着升级if语句的所有实例. - I can't create generic
SELECT count(*) FROM table
queries as I have too many specific select statements. - I could run a
preg_replace
to remove everything betweenSELECT and FROM
and then replace with aCOUNT(*)
and run a second query but this assumes all queries are setup a certain way. - I could
fetchAll
first thencount()
the results but that means upgrading all instances of the if statements.
那么,如果人们将他们的代码更新为 PDO,那么什么是最好的替代 *_num_rows 函数的方法.不是解决特定问题的东西,而是替代 *_num_rows 功能的东西.如果这是不可能的,以前是什么让它成为可能的?
So what is the best all around replacement to a *_num_rows function if people are updating their code to PDO. Not something that solves a specific problem, but something that replaces the functionality of *_num_rows. If that's not possible what allowed it to be possible before?
推荐答案
如果你想计算行数,你可以使用 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);
如 SELECT 语句与 PDO 一起使用时,无法直接计算行数.rowcount.php" rel="nofollow noreferrer">文档.
There is no way to directly count rows when using a SELECT
statement with PDO as stated in the docs.
PDOStatement::rowCount() 返回受相应的最后一个 DELETE、INSERT 或 UPDATE 语句影响的行数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.
这篇关于相当于 mysql_num_rows 或 mssql_num_rows 的 PDO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:相当于 mysql_num_rows 或 mssql_num_rows 的 PDO
基础教程推荐
- 使用 PDO 转义列名 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01