1

Possible Duplicate:
How can I fetch the last row I inserted using DBI?

I need to be able to call functions in mysql. One example is:

$dbh = DBI->connect(......)
$sqlQuery = "INSERT INTO xxx VALUES ......";
$sth = $dbh->prepare($sqlQuery);
$sth->execute();

#The missing code is here
#Call the function mysql_insert_id to retrieve the id of the last inserted record
#Do something with the id

How can I do this?

Community
  • 1
  • 1
A. B
  • 687
  • 9
  • 18
  • Thanks Mat. I used the solution suggested in the indicated post which is: $insertedId = $sth->{mysql_insertid} – A. B Dec 15 '12 at 16:14

2 Answers2

3

from perldoc DBI:

$rv = $dbh->last_insert_id($catalog, $schema, $table, $field);
b0fh
  • 1,678
  • 12
  • 28
2

documentation: https://metacpan.org/pod/DBI#last_insert_id

my $id = $dbh->last_insert_id();

parameters are ignored for MySQL

titanofold
  • 2,852
  • 1
  • 15
  • 21
carillonator
  • 4,715
  • 3
  • 29
  • 39
  • I'm getting: Can't locate object method "mysql_insert_id" via package "DBI::db" at ... – A. B Dec 15 '12 at 16:10
  • it's `last_insert_id`, not `mysql_insert_id` – carillonator Dec 15 '12 at 16:35
  • oh sorry, my bad. It seems though that ignoring the arguments depends on the driver version coz in my case it complained about the number of arguments. Thanks a lot. – A. B Dec 15 '12 at 16:51