Can i get the `insert_id` (actually just the `id`) when inserting with `ON DUPLICATE KEY`?

24 views Asked by At

I am using MySQLi in PHP, and as it turns out, when performing a INSERT query (via MySQLi::query()) with a ON DUPLICATE KEY UPDATE syntax, the insert_id property on the MySQLi object does not get updated with the ID of the entity if you try to insert something that violates the UNIQUE index on the database.

I would really like to avoid a second query, but i haven't been able to find in the documentation if there is any alternative way to reach the ID of the entity whose UNIQUE index has been broken (which is essentially the entity i am trying to insert, so i want to be able to tie it to that ID)?

Just to clarify, i am aware i can get the id if i want to, with a second (SELECT) query, but i am wondering if there is some metadata on the MySQLi object that would allow me to get the id of the entity in question, without doing a second query?

0

There are 0 answers