Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

How do I find the value MYSQL assigned to an AUTO_INCREMENT ID (int) Field?

by Anonymous Monk
on Jun 19, 2000 at 04:13 UTC ( [id://18749]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm using the DBI to INSERT into a MYSQL database.
There is an ID row which is a Primary Key field which has the
AUTO_INCREMENT property.

After doing an INSERT statement, how can I find out the
value assigned to the ID for the new entry? I'd like to
avoid having to do a SELECT statment, and thus access
MYSQL again, for this information.

thanks
benji
  • Comment on How do I find the value MYSQL assigned to an AUTO_INCREMENT ID (int) Field?

Replies are listed 'Best First'.
Re: How do I find the value MYSQL assigned to an AUTO_INCREMENT ID (int) Field?
by plaid (Chaplain) on Jun 19, 2000 at 04:23 UTC
    From the DBD::mysql man page:
    $insertId = $dbh->{'mysql_insertid'}
Re: How do I find the value MYSQL assigned to an AUTO_INCREMENT ID (int) Field?
by Ovid (Cardinal) on Jun 19, 2000 at 04:25 UTC
    I haven't tested it, but you should be able to use the following:
    #!/usr/bin/perl # connect to database .... my $sql_statement = "INSERT INTO $table (field1,field2) VALUES($value1,$value2)"; my $sth = $dbh->prepare($sql_statement); $sth->execute or die "Can't Add record : $dbh->errstr"; # Now we can retrieve the primary key that # was just created in our last insert. my $table_key = $sth->{insertid}; # or you could use this method which # is DBI-standard my $table_key = $dbh->{'mysql_insertid'}; $sth->finish;
    From the Giving Credit where Credit is Due department: I got this from this link.
RE: How do I find the value MYSQL assigned to an AUTO_INCREMENT ID (int) Field?
by chromatic (Archbishop) on Jun 19, 2000 at 04:26 UTC
    You can use the LAST_INSERT_ID() function. That's a MySQL specific function, so you'll probably be better off at their site for details. (I believe you can call it in a normal UPDATE statement.)

    Looking at the DBI-provided functions, though, I'd use them.

RE: How do I find the value MYSQL assigned to an AUTO_INCREMENT ID (int) Field?
by Russ (Deacon) on Jun 19, 2000 at 06:23 UTC
    Just as a "dot all the i's and cross all the t's" follow-up to plaid's and Ovid's posts...

    If you are still in the stone ages (like me) and have a version of mysql older than 3.22.2? (23, I think), you must use $sth->{insertid}. Newer versions warn us that this is deprecated. Newer versions should use $sth->{mysql_insertid}.

    Not trying to muddy the waters, but hoping to prevent confusion for people too lazy to upgrade (like me). :-)

    Russ

Re: How do I find the value MYSQL assigned to an AUTO_INCREMENT ID (int) Field?
by Brovnik (Hermit) on Jun 05, 2001 at 15:57 UTC
    I use :
    my $sth = $dbh->prepare($sql); $sth->execute(); my $id = $sth->{'mysql_insertid'};
    for me, $dbh->{'mysql_insertid'} doesn't work, (always returns 0).
    --
    Brovnik
Re: How do I find the value MYSQL assigned to an AUTO_INCREMENT ID (int) Field?
by nmerriweather (Friar) on Mar 31, 2005 at 04:47 UTC
    FYI Using DBD::mysql 2.9004
    Works
    $id = $dbh->{mysql_insertid} $id = $dbh->{insertid} $id = $sth->{mysql_insertid}
    Does Not Work
    $id = $sth->{insertid}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://18749]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-18 19:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found