2011-05-13 14:44

如何在PHP中找到查询“INSERT ... ON DUPLICATE KEY UPDATE ...”的哪一部分成功了?


I have MySQL table that doesn't have AUTO_INCREMENT field. In PHP I run this simple query:

mysql_query("INSERT INTO table ... ON DUPLICATE KEY UPDATE ....");

I need to be able to detect if a new row was inserted into table or if an old row was updated ? Is it possible somehow ?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • drt41563 drt41563 10年前

    mysql_affected_rows() returns 1 on an insert (because it inserted one row) and 2 on an update (because first it tried to insert, and then it updated, or something like that).

    That is when you're just trying to add one row. You can have the ON DUPLICATE KEY UPDATE clause on multi-row inserts too, and then you'll get the sum of 1's and 2's for all the rows.

    点赞 评论 复制链接分享