Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Getting last MySQL id with Class::DBI

by Phaysis (Pilgrim)
on May 20, 2003 at 14:02 UTC ( [id://259451]=note: print w/replies, xml ) Need Help??


in reply to Getting last MySQL id with Class::DBI

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?

Log In?
Username:
Password:

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

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

    No recent polls found