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.


In reply to Re: Getting null value from last_insert_id() by ferreira
in thread Getting null value from last_insert_id() by chiragforever

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.