5 messages in com.mysql.lists.mysqlRE: how to get the id of a newly inse...
FromSent OnAttachments
ji...@work12 Jan 2000 17:34 
Paul DuBois12 Jan 2000 18:23 
Jamie Le Tual12 Jan 2000 20:59 
Nicolas Prade13 Jan 2000 02:46 
Paul DuBois13 Jan 2000 04:46 
Subject:RE: how to get the id of a newly inserted record???
From:Nicolas Prade (pra@internetassocies.com)
Date:01/13/2000 02:46:08 AM
List:com.mysql.lists.mysql

ok, i'm sure i have missed this in previous discussions, and it seems so elemetary (uh, ya) that i must have overlooked it in the manual (albeit, it's huge). when i insert a record into the database from PERL (using DBI), that has an auto-increment primary key, how do i got about getting that primary key value??

In SQL: SELECT LAST_INSERT_ID() In DBI: $dbh->{mysql_insert_id} In PHP: mysql_insert_id() Of course, you can use the SQL statement in either DBI or PHP, too.

Hello, just one precision/warning, from the doc:

" The most recently generated ID is maintained in the server on a per-connection basis. It will not be changed by another client. It will not even be changed if you update another AUTO_INCREMENT column with a non-magic value (that is, a value that is not NULL and not 0). "

This means a little more work :-) for those of us using only 1 connection for all the clients, for example by setting up the connection in a Java servlet.