in reply to How do I find what column $dbh-{'insert_id'} was inserted into?

Thanks for all of the help. I appologize because I should have actually said $dbh->{'mysql_insertid'}, sorry for the confusion. What I ended up doing is writting a quick function to look at the table and return the first auto_increment field it found.

This is what I came up with:
sub get_auto_increment { my ($dbh, $table) = @_; my $sth = $dbh->prepare("show columns from $table"); $sth->execute; my $ref; while ($ref = $sth->fetchrow_hashref) { if ($ref->{'Extra'} eq 'auto_increment') { $sth->finish; return $ref->{'Field'}; } } $sth->finish; return undef; }

The idea is that the first column that matches 'auto_increment' will be the column that $dbh->{'mysql_insertid'} is referring to. I am not sure what the rules are for mysql's auto_increment definition. If I am not mistaken (which I probably am) all columns marked auto_increment would get the same value anyway?

Thanks,
-Art-
  • Comment on Re: How do I find what column $dbh-{'insert_id'} was inserted into?
  • Download Code

Replies are listed 'Best First'.
Re: Re: How do I find what column $dbh-{'insert_id'} was inserted into?
by dvergin (Monsignor) on Feb 14, 2001 at 23:50 UTC
    Actually, MySQL only allows one auto_increment column per table. So your method should work fine.