Or, you could do what I do:

Before I execute the insert query, I put a write lock on the table, perform the insert, and then do a select query for the id of the most recently inserted row (using a column in my table definition of the type timestamp and the clause ORDER BY modified DESC LIMIT 1). I then release the write lock allowing other processes to do their thing.

Or, instead of relying on a timestamp column, you could possibly just order by the primary key, descending. This should return the highest key id, which'll possibly be the most-recently inserted row.

This 4-step process works well for me, and from what I've gathered it's fairly portable across database engines.

-Shawn / (Ph) Phaysis
If idle hands are the tools of the devil, are idol tools the hands of god?


In reply to Re: Getting last MySQL id with Class::DBI by Phaysis
in thread Getting last MySQL id with Class::DBI by broquaint

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.