Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Getting null value from last_insert_id()

by ferreira (Chaplain)
on Apr 05, 2007 at 11:09 UTC ( #608447=note: print w/replies, xml ) Need Help??

in reply to Getting null value from last_insert_id()

If that:
$tmp_query="insert into <TableName> values('','');"; $sth = $dbh->prepare($tmp_query); $sth->execute(); $sth->finish();
looks like your actual code, you have some problems to work out before getting something from last_insert_id().

First, DBI drivers don't expect ';' ending the statements most of the time (even though I don't know especifically about mysql). I would suggest to remove this terminator.

Second, if you are not using RaiseError => 1, every problem/error is passing unnoticed if you don't check the return of DBI methods like prepare, execute and finish. One of the easiest ways is to add RaiseError => 1 to your connection creation and let DBI throw an exception if something goes wrong.

After getting rid of these issues, you may try with more confidence to interpret the results of last_insert_id() (and it is possible it just works then and does not need more tweaks). Also consider Re: Getting null value from last_insert_id() and make sure you need last_insert_id after all.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://608447]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2019-10-18 09:24 GMT
Find Nodes?
    Voting Booth?