0

Sorry if the title is unclear. Basically I have a query that inserts multiple rows like this:

//Build Query
$querySQL = ('
    INSERT INTO myTable (col1, col2)
    VALUES (val1, val2), (val1, val2);
');
//Run query
$db->query($querySQL);

//Get the last inserted id
$last_id = $db->insert_id;
echo $last_id;

When I run this query, I will insert two rows into the "myTable" table. Let's say the id's are k and k+1. You would expect that the code would print k+1 but it doesn't! It prints k.

How come? And how safe is it to assume that the return value of insert_id will always be the first row inserted when inserting in this way?

Thanks!

hichris123
  • 10,145
  • 15
  • 56
  • 70
Ethan Melamed
  • 367
  • 1
  • 11
  • Obviously retrieving the insert id only makes sense when you are inserting exactly one row. So whatever you're doing, you need to rethink it. – developerwjk Nov 12 '15 at 21:35
  • It is safe to assume that yes, check out the accepted answer at http://stackoverflow.com/questions/4637367/mysql-last-insert-id-used-with-multiple-records-insert-statement. – robbymarston Nov 12 '15 at 21:55

0 Answers0