How can I fetch the last row that was inserted using DBI (DBD::mysql)?
Code sample:
my $sth = $dbh->prepare(\'INSERT INTO a ( x, y, z ) VALUES ( ?
A database agnostic approach is to use the DBI's last_insert_id method. This approach helps to reduce dependency on a specific database:
$dbh->last_insert_id
$rv = $dbh->last_insert_id($catalog, $schema, $table, $field);
Returns a value 'identifying' the row just inserted, if possible. Typically this would be a value assigned by the database server to a column with an auto_increment or serial type. Returns undef if the driver does not support the method or can't determine the value.
This is a property of the statement handle. You should be able to access the ID like that:
$sth->{mysql_insertid}
SELECT LAST_INSERT_ID()
query will also return what you want.