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.
Getting Primary ID of row just inserted into MySQL
Moderator: General Moderators
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
Re: Getting Primary ID of row just inserted into MySQL
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
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
example:
insert record here
call most recent record id here
Thank you
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
Re: Getting Primary ID of row just inserted into MySQL
Your example is correct
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: Getting Primary ID of row just inserted into MySQL
mysql_insert_id() fetches the last inserted id from your connection
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Re: Getting Primary ID of row just inserted into MySQL
Take note that this function does not work when calling it against an insert stored procedure.