how to know if last operation was insert or update when using insert on duplicate update in mysql and php?
-
12-09-2019 - |
Question
I am using PHP and MySQL.
If I use an INSERT ON DUPLICATE UPDATE
SQL statement, then how do I know if the last operation was an successful insert and not an update or unsuccessful insert?
The assumptions are the table in question does not use an auto increment, so I can't use a mysql_insert_id
to help me find out.
Solution
You'll want to check mysql_affected_rows() which will return 1 on an insert and 2 on an update. As per the mysql documentation.
if (mysql_affected_rows() == 1) //INSERT
elseif (mysql_affected_row() == 2) //UPDATE
Unless the update does not change anything, in which case it will return 0.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow