How to know if when using “on duplicate key update” a row was inserted or updated?

You can get this information at the time of the insert/update by examining the number of affected rows in the result set.

MySQL documentation states:

With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if
the row is inserted as a new row and 2 if an existing row is updated.

You’ll need to combine ROW_COUNT with LAST_INSERT_ID to get your answer and insert one row at a time.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)