2

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.

Carvell Fenton
  • 2,341
  • 6
  • 23
  • 30

1 Answers1

2

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.

mauris
  • 42,982
  • 15
  • 99
  • 131