Getting the id of the record just added with prepared mysql statements

I insert a record using PDO and store the result in $result

which I use as boolean

$result = $addRecord->execute();

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

      

I would also like to add the entry I just added id

. In a table, each record has a field auto_incremented

called id

. I tried doing this

$new_id = $result['id'];

      

but it $result

doesn't seem to actually add an entry. Can someone confirm this and how can I access the newly added entry?

Please note that multiple people can add to the same table at the same time, so I need something really accurate.

+2


a source to share


1 answer


PDO :: lastInsertId () should work.

EDIT: (didn't see your other part)



MySQL maintains the latest insert identifier based on http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html "> per-connnection. So if something else inserts a row it should return only unexpected results if this query was executed using the same connection (for example a persistent connection).

+4


a source







All Articles