[UPHPU] Sequentially number records with php/mysql

David Smith DavidSmith at byu.net
Wed Nov 10 11:43:40 MST 2004


<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 it.

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.

--Dave



More information about the UPHPU mailing list