Search code examples
phpmysqlunique-keyrows-affected

mysql insert on duplicate key update, check which one occurred


In PHP, how to detect which one happened (INSERT or UPDATE) in the following query:

INSERT INTO ... ON DUPLICATE KEY UPDATE ...

Solution

  • From the PHP manual mysql_affected_rows :

    In the case of "INSERT ... ON DUPLICATE KEY UPDATE" queries, the return value will be 1 if an insert was performed, or 2 for an update of an existing row.

    So using the function mysql_affected_rows() after execution of the query, it can be detected from the returned value of the function.