Corion is right that the method should be called on $dbh (though in my test it worked on $sth too, so I'm not sure that's the problem here). Note the documentation: "The last_insert_id method was added in DBI 1.38." - what version do you have?
use warnings; use strict; use feature 'say'; use DBI; my $dbh = DBI->connect( "DBI:mysql:database=testing;host=127.0.0.1", $ENV{USER}, 'barfoo', { RaiseError => 1, AutoCommit => 1 }); $dbh->do('DROP TABLE IF EXISTS coacttran'); $dbh->do(<<'ENDSQL'); CREATE TABLE coacttran ( id INT PRIMARY KEY AUTO_INCREMENT, foo VARCHAR(256) ); ENDSQL for my $i (1..10) { my $req = qq{ INSERT INTO `coacttran` SET foo=? }; my $sth = $dbh->prepare($req); my $row = $sth->execute("bar$i"); say $dbh->last_insert_id; }
I spun up the test database with Docker the same way I showed here.
Also note that I don't know how you're generating $_pstmt, but you really should use placeholders! See Bobby Tables.
In reply to Re: last_insert_id did not work
by haukex
in thread last_insert_id did not work
by bizactuator
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |