PDOStatement::rowCount result when used after PDO::commit?

蹲街弑〆低调 提交于 2019-12-06 23:49:17

问题


In the MySQL docs, there is a note about using mysql_affected_rows after a transaction commit: http://php.net/manual/en/function.mysql-affected-rows.php

Note: Transactions
If you are using transactions, you need to call mysql_affected_rows() after your INSERT, UPDATE, or DELETE query, not after the COMMIT.

However, there is no such note on the PDOStatement::rowCount doc: http://www.php.net/manual/en/pdostatement.rowcount.php

Does this mean the commit will not affect the affected rows count after INSERT, UPDATE or DELETE queries when using the PDO object?


回答1:


A PDOStatement is returned for each query that is executed. You will be able to use PDOStatement->rowCount() at any time in your code (during or after a transaction and rollback/commit doesn't matter). Each object takes care of maintaining itself.

The reason mysql_affected_rows has that transaction note is because it is only aware of a single mysql connection resource. This means that when you complete the transaction (commit/rollback) a new query has been sent to the DB, thus altering which result is being processed for the number of affected rows.



来源:https://stackoverflow.com/questions/10272134/pdostatementrowcount-result-when-used-after-pdocommit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!