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条)

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法