last_insert_id()
$dbh->last_insert_id($catalog,$database,$table,$column[,\%attr])
This function returns the value stored in the row
identification column of the most recent row inserted for the current
MySQL session, provided the identification number was incremented
using AUTO_INCREMENT in MySQL. It works like the
LAST_INSERT_ID() function in MySQL. No
arguments for this function are necessary with MySQL: if given, their
values are ignored, although undef is required at a
minimum. Other systems may require other options. This function
doesn’t work with MySQL before version 1.45 of DBI. It returns
undefined if it cannot retrieve the number (which must be retrieved
after the insert, and before another statement in MySQL) or if the
driver does not support this function.
Here is an example:
...
my $sth = $dbh->prepare("INSERT INTO books (title, author)
VALUES (?,?)");
$sth->execute($title,$author);
my $book_id = $dbh->last_insert_id(undef,undef,undef,undef,undef);
print "New Book ID: $book_id \n";
$sth->finish();