我经常像这样使用 PDO 的 rowCount 函数,例如:
$sql = $dataBase->prepare('SELECT email, firstname, lastname
FROM pr__user
WHERE id = :id');
$sql->execute(array('id' => $_SESSION['user_id']));
$rowCount = $sql->rowCount();
它一直运行良好,但我在 PHP 手册中看到:
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. http://php.net/manual/en/pdostatement.rowcount.php
它适用于 MySQL 和 MariaDB,所以我继续使用它。当我使用它作为我希望可移植的应用程序时,我应该修改我的代码吗?
最佳答案
我从不询问行数。查询总是返回一个结果行数组(以某种格式),我可以简单地询问数组中有多少行——例如使用 PHP 的 count(..)
函数。
关于php - 经常使用的 PDO 行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41926623/