|
-
Mar 17th, 2008, 12:43 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Get the PK of a auto increment value you just inserted
right now my program inserts a new record and a PK of int is automatically created. But I also need a second query to insert data into another table that has a 1:1 relationship.
Is it possible to somehow get the auto increment value that was created from my insert statement? Or should I simple do another select query to get the latest entry inserted?
I guess for now ill use
Code:
SELECT max(ID) FROM tableName
Last edited by Zeratulsdomain; Mar 19th, 2008 at 02:47 PM.
-
Mar 17th, 2008, 01:00 PM
#2
Re: Get the PK of a auto increment value you just inserted
There are a few methods explained in the article How can I get the AutoNumber/Identity value for the record just added? from our Database Development FAQs/Tutorials (at the top of the Database Development forum)
-
Mar 17th, 2008, 05:05 PM
#3
Re: Get the PK of a auto increment value you just inserted
I've always gotten it before the query.
PHP Code:
$r = mysql_query('SHOW TABLE STATUS LIKE "table_name"') or die(mysql_error()); $d = mysql_fetch_array($r, MYSQL_ASSOC); $next_id = $d['Auto_increment']; unset($d); mysql_free_result($r);
I'm by no means an expert at PHP so take this with a grain of salt...
-
Mar 17th, 2008, 06:53 PM
#4
Re: Get the PK of a auto increment value you just inserted
Use the mysql_insert_id function to retrieve the auto_increment ID generated by the last INSERT operation.
Because this function is connection-specific, there are no concurrency issues.
The SELECT MAX() and SHOW TABLE STATUS methods are both fallible: both methods require two queries and are thus susceptible to concurrency issues. (What if someone inserts a row while you're busy working out the next ID?)
Further, the maximum value of a column is not necessarily the next auto_increment value. MAX() might return one value, but you could set another by using ALTER TABLE table_name AUTO_INCREMENT=x.
If you need to insert into multiple tables at once, I would use a stored procedure. This minimises traffic between the web and the database server. For a non-trivial project, I would also suggest ditching the standard php_mysql library and using a more advanced data access library such as PDO (PHP 5, abstracted) or MDB2 (PHP 4/5, abstracted) or mysqli (PHP 4/5, non-abstracted). These support useful features such as parameters and prepared statements.
Last edited by penagate; Mar 17th, 2008 at 06:56 PM.
-
Mar 19th, 2008, 02:47 PM
#5
Thread Starter
Hyperactive Member
Re: Get the PK of a auto increment value you just inserted
 Originally Posted by penagate
Use the mysql_insert_id function to retrieve the auto_increment ID generated by the last INSERT operation.
Because this function is connection-specific, there are no concurrency issues.
The SELECT MAX() and SHOW TABLE STATUS methods are both fallible: both methods require two queries and are thus susceptible to concurrency issues. (What if someone inserts a row while you're busy working out the next ID?)
Further, the maximum value of a column is not necessarily the next auto_increment value. MAX() might return one value, but you could set another by using ALTER TABLE table_name AUTO_INCREMENT=x.
If you need to insert into multiple tables at once, I would use a stored procedure. This minimises traffic between the web and the database server. For a non-trivial project, I would also suggest ditching the standard php_mysql library and using a more advanced data access library such as PDO (PHP 5, abstracted) or MDB2 (PHP 4/5, abstracted) or mysqli (PHP 4/5, non-abstracted). These support useful features such as parameters and prepared statements.
mysql_insert_id works, ill use that, tnx.
As for my last method, I am aware of the potential problem hence why I made this thread. I figured there was about a < 0.001% chance of it happening in this situation but I also wanted to know the proper way of doing it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|