douyong8801 2011-05-13 14:44
浏览 84
已采纳

如何在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 ?

  • 写回答

1条回答 默认 最新

  • drt41563 2011-05-13 15:08
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?