Getting the number of rows affected by a MySQL update is one thing, but how do I get the number of matching rows for an UPDATE query? Otherwise put, the number of rows that COULD have been affected by the update query, even if the data I'm trying to update the rows with is identical to the data already contained within them? Because in this specific case, the DB will not overwrite the data with, so the number of affected rows is 0.
Is there some PDO method that helps, similar to PDOStatement::rowCount()
?
I also want to avoid a secondary SQL statement, otherwise the question is pointless.
如何获取可能受UPDATE影响的行数?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
4条回答 默认 最新
- dsbo44836129 2013-11-19 09:05关注
I wanted to avoid extra queries if possible and fortunately, a colleague of mine pointed out that I can give the PDO instance a particular MySQL driver option that forces the
rowCount
method to return the number of matches related to the query, instead of the number of affected rows, like so:$options = array( PDO::MYSQL_ATTR_FOUND_ROWS => true // forces the rowCount() method to return the number of matching rows, not the number of affected rows ); $connection = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass, $options);
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报