Re: Re: Getting last record ID created from DB

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, March 17, 2007 9:43 am, Myron Turner wrote:
> An earlier post called attention to a concurrency problem.  Wouldn't
> getting the last inserted ID from LAST_INSERT_ID()
> suffer from the same limitations as any of the other solutions which
> do
> a select to get the last ID?  I assume if you are completely in
> control
> of the database, you could create a lock file using flock() and remove
> the lock once the Id is retrieved using any of these methods.  I guess
> what I'm wondering is whether the simplest suggestion is the one that
> would use the email address as a condition in the WHERE clause to
> extract the ID?

No, no, and no.

The MySQL developers spent a zillion hours making the LAST_INSERT_ID()
function be tied to YOUR database connection.

You get *your* LAST_INSERT_ID(), not some random one from some other
database connection.

That's why http://php.net/mysql_insert_id takes the connection as an
argument.  Okay, so it's an optional arg, and PHP uses the last-used
connection by default, but that's another issue entirely...

Use the function provided, which was expressly designed to avoid the
concurrency problem, which already has all the locking and whatnot
built into it.

Do not roll your own.

You will only end up re-inventing a wheel, at best, and probably just
screwing it up, most likely.

These guys have spent a good chunk of their lives figuring this stuff
out and becoming experts on it.  Let them expertise.

And to address a very specific suggestion: You never know when some
goof is going to register twice with the same email, possibly hitting
re-load, and so that's not a Good Idea.

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux