Search code examples
perlmariadb

Mysql to MariaDB mysql_insertid - LAST_INSERT_ID issue


My host recently switched from mysql to mariadb. Most everything transitioned well except the:

$id = $sth->{mysql_insertid};

This is a perl cgi.pm script. My code is:

my $dbh = DBI->connect("DBI:MariaDB:database=$database;host=localhost", $MYSQLuserid, $MYSQLpassword, { RaiseError => 1, PrintError => 0 });
my $sth = $dbh->prepare(qq(INSERT INTO mytable VALUES ($questionmarks) )) or die $DBI::errstr;
$sth->execute(@new_row) or die $DBI::errstr; #question marks and new_row were never the problem
$id = $sth->{mysql_insertid};  # <<<<<<<< culprit
$sth->finish() or die $DBI::errstr;

This is the error:

DBD::MariaDB::st FETCH failed: Unknown attribute mysql_insertid at my.cgi line 23234

If you can provide actual code that would be great. If auto increment needs to be changed, I will do that too. Any help is appreciated

I've tried these:

$id = $sth->{LAST_INSERT_ID};
$id = $sth=>LAST_INSERT_ID(mutable);
$id = mysqli_use_result($sth->$dbh);
$id =(LAST_INSERT_ID(), 1);
SELECT LAST_INSERT_ID();
$id = $sth->LAST_INSERT_ID();
$id = $sth->mysqli_insert_id();
$id = mysqli_insert_id();
SET @id = LAST_INSERT_ID();

Solution

  • You can use $sth->last_insert_id() or $dbh->last_insert_id()