[UPHPU] Sequentially number records with php/mysql

Brent Wilkinson brent at air2data.com
Wed Nov 10 12:32:00 MST 2004

Didn't say was the best way to do just another possible way that very easy
could have tons of issues associated with it and I did it off the top of my
head. Mysql autoincrement is still the best way to do it. 

Brent Wilkinson
Tech Manager

-----Original Message-----
From: uphpu-bounces at uphpu.org [mailto:uphpu-bounces at uphpu.org] On Behalf Of
David Smith
Sent: Wednesday, November 10, 2004 11:44 AM
To: uphpu at uphpu.org
Subject: RE: [UPHPU] Sequentially number records with php/mysql

<quote who="Brent Wilkinson">
> Well you could do it this way.
> function getNextUserId()
> {
>  $result = mysql_query("select max(user_id) from user") or die("Unable 
> to get next user id");  $id = mysql_result($result, 0) + 1;  return 
> $id; }
> This is clunky though and the autoincrement way is the best and 
> easiest way to do it. I would also probably just add a extra field in 
> the db and mark a order inactive or deleted instead of actually deleting

Not only is this clunky, it is incorrect. This code is subject to a race
condition, and can lead to duplicate user_id values. I will leave the
details to the readership, but if you're curious, please inquire.



UPHPU mailing list
UPHPU at uphpu.org
IRC: #uphpu on irc.freenode.net

Sponsored by hostinginferno.com!

More information about the UPHPU mailing list