Archive for the ‘Perl’ Tag

Perl DBI Last Insert ID

Recently I was re-searching for an Perl DBI corenspondent for PHP’s last_insert_id(), so here’s what I’ve found:

$dbh->do('INSERT INTO some_table ...');
my $id = $dbh->last_insert_id(undef, undef, qw(some_table some_table_id)) or die "can not get it?";

Another way:

my $id = $dbh->{ q{mysql_insertid}};

The two undefs are for catalog and schema which don’t appear to be necessary. The DBI docs say they can be ignored for MySQL so you might not even need the table and field parameters.