phpmysqlmysqliinsert-id

PHP MySQLi and MySQLi_STMT: Which insert_id to use?


Both the MySQLi and MySQLi_STMT classes have an $insert_id property.

If I am connected to my database using a MySQLi object (say $db), and then I perform an INSERT with a MySQLi_STMT object (say $stmt), to get the id of the last INSERT, should I use:

$last_id = $db->insert_id;

or

$last_id = $stmt->insert_id;

Or would they be the same, in which case it doesn't matter?

I thought this might be a quick answer for someone, and save me the time of writing the test code to check it.

Thanks in advance as always.


Solution

  • I would assume that both are the same, if read at the same time.

    However if you execute another INSERT statement, the $db->insert_id might change.