Page 1 of 1

Getting Primary ID of row just inserted into MySQL

Posted: Thu Apr 10, 2008 6:34 am
by real246
How do I retrieve the Primary ID of a row that I just inserted into a MySQL v5 table when the primary ID is set to auto increment and the value is set to INT of the primary ID?

Any help would be greatly appreciated.

Thank you.

Re: Getting Primary ID of row just inserted into MySQL

Posted: Thu Apr 10, 2008 6:47 am
by aceconcepts
The following will return the most recent mysql insert:

Code: Select all

 
$id=mysql_insert_id();
 

Re: Getting Primary ID of row just inserted into MySQL

Posted: Thu Apr 10, 2008 7:53 am
by real246
can i be reasonably guaranteed that if I put the INSERT record a few lines before I call the most recent mysql insert that it will be the one that I just inserted? Reason I ask is because the database is being updated simultaneously by other users constantly. Wondering if this might possibly interfere?

example:

insert record here

call most recent record id here


Thank you

Re: Getting Primary ID of row just inserted into MySQL

Posted: Thu Apr 10, 2008 8:22 am
by aceconcepts
Your example is correct

Re: Getting Primary ID of row just inserted into MySQL

Posted: Thu Apr 10, 2008 9:08 am
by John Cartwright
mysql_insert_id() fetches the last inserted id from your connection

Re: Getting Primary ID of row just inserted into MySQL

Posted: Thu Apr 10, 2008 3:51 pm
by RobertGonzalez
Take note that this function does not work when calling it against an insert stored procedure.