Getting the record ID just added with mysql prepared statements

Posted by dmontain on Stack Overflow See other posts from Stack Overflow or by dmontain
Published on 2010-05-09T00:25:19Z Indexed on 2010/05/09 0:28 UTC
Read the original article Hit count: 478

I'm inserting a record using PDO (very similar to mysqli).

$addRecord->execute();

To know if the operation worked, I've learned that I can save it to a variable $result that can be used as true false

$result = $addRecord->execute();

if ($result){ 
   //add successful
} else {
   //add unsuccessful
}

What I'd like to do is also get the record id just added. In the table, each record has an auto_incremented field called id. I tried doing this

$new_id = $result['id'];

but it seems that $result is purely boolean and doesn't actually hold the actual record that was added. Can someone confirm this and how would I then access the record just added? Note that several people may be adding to the same table at the same time, so I think getting just the last one would not be very accurate.

© Stack Overflow or respective owner

Related posts about mysql

Related posts about database