Getting the record ID just added with mysql prepared statements
- by dmontain
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.