我知道以前有人问过这个问题,但似乎解决方案是针对所提出的问题的.
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语句的所有实例.
SELECT count(*) FROM table
queries as I have too many specific select statements.preg_replace
to remove everything between SELECT and FROM
and then replace with a COUNT(*)
and run a second query but this assumes all queries are setup a certain way.fetchAll
first then count()
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!