问题
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