Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ...
like image 490
Aliweb Avatar asked Jan 11 '23 01:01

Aliweb


1 Answers

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.

like image 79
Aliweb Avatar answered Jan 12 '23 13:01

Aliweb