Re: Getting last record ID created from DB
| From: | Richard Lynch | Date: | Tue, 20 Mar 2007 00:14:07 +0000 |
| Subject: | Re: Getting last record ID created from DB | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-250855@lists.php.net to get a copy of this message | ||
On Fri, March 16, 2007 10:37 pm, Rod Person wrote:
> select user_id from t_users where user_id = (select max(user_id) from
> t_users)
Noooooooooooooooooooooo!
This is a "race condition"
Imagine, if you will, that *TWO* users sign up and create characters
on your site at the exact same time:
Lee Francis
insert into user... insert into user...
auto_inc: #5 auto_inc: #6
select max(user_id) select max(user_id)
#6 #6
You'll tell BOTH of them that they are #6, even though Lee was really #5.
Yes, this can easily happen.
In fact, on a moderately busy site, it *WILL* happen quite quickly.
You REALLY do want to use http://php.net/mysql_insert_id to find out
the last ID used FOR THIS CONNECTION.
The MySQL guys worked really really really hard to do this right.
Don't be an [bleep] and not use their hard-won experience.
--
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?