douming4359 2013-11-19 07:52
浏览 1068
已采纳

如何获取可能受UPDATE影响的行数?

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.

  • 写回答

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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样