If this is your first visit, be sure to check out the FAQ by clicking the link above.
You may have to register before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
how to get the auto increment integer primary key value of a new record
I have a DB table that has a auto increment integer primary key, if I insert a new record into it by SQL statement "INSERT INTO...", how can I get the integer primary key value of that newly created record?
For this purpose, you can use mysql_insert_id().
It returns the value generated for an auto_increment column by the previous INSERT or UPDATE statement.
Full documentation to be found in the mySQL manual that can be downloaded from www.mysql.com.
mySQL keeps track of more people doing simultaneous inserts. That's why you should NOT use a SELECT MAX(KEY) FROM TABLE because someone else may have inserted a record just after you, and will get the wrong key value.
mySQL links your particular session to the actions that you have executed. By doing so, mysql_insert_id() will return YOUR inserted key, no matter how many people are inserting at that moment.