7 messages in com.mysql.lists.perlRe: $dbh->last_insert_id() not wor...
FromSent OnAttachments
Daniel Kasak15 Jun 2004 16:10 
William R. Mussatto15 Jun 2004 16:21 
Daniel Kasak15 Jun 2004 16:39 
William R. Mussatto15 Jun 2004 17:02 
Jochen Wiedmann15 Jun 2004 23:40 
Daniel Kasak16 Jun 2004 15:20 
lei shen17 Jun 2004 07:44 
Subject:Re: $dbh->last_insert_id() not working
From:William R. Mussatto (muss@csz.com)
Date:06/15/2004 04:21:00 PM
List:com.mysql.lists.perl

Daniel Kasak said:

Hi all.

I'm writing a helper object to synchronise data in DB server with fields on a glade-generated form. Part of that involves handling inserts:

eval { $sth->execute (@bind_values) || die $dbh->errstr; };

# If the above failed, there will be something in the special variable $@ if ($@) {

# Dialog explaining error... my $dialog = &msgbox( $prospects->get_widget("Prospects"), "Error updating recordset!", "Database Server says:\n" . $dbh->errstr, 1 ); $dialog->run; $dialog->destroy; warn "Error updating recordset:\n$sql\n@bind_values\n" . $@ . "\n\n"; return 0;

}

$pk = $dbh->last_insert_id();

When the above code runs, a record is inserted correctly - perfectly in fact. The 'insert into' statement appears immediately in the MySQL query log. The problem is that the last line doesn't get a value - $pk is undef.

I haven't yet tried doing a 'select last_insert_id()' or 'select @@identity' because I'm trying to keep the code as portable as possible - so it can be used with other DB servers, and the obvious way of doing this is by letting DBI take care of the details ( if it worked ).

Anyone know what's up?

syntax error: should be $pk = $dbh->{'mysql_insertid'};