Re: Random Unique ID

From: Date: Fri, 23 Mar 2007 18:48:07 +0000
Subject: Re: Random Unique ID
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-251207@lists.php.net to get a copy of this message
Robert Cummings writes:
Bleh, you can solve this with at most 2 queries.
Thanks. That is a bit different than what I was thinking about but it might work for my purposes. Thanks also to all the other people who made suggestions. It appears to me that ther are 3 distinct approaches. 1) Generate a random number. Query the DB to see if it has been used before and if so generate another random number and repeat. It seems to me that this would require that the DB be locked for the checking and insertion to make sure a duplicate was not slipped in. 2) Generate a random number. Make a trial insertion for a unique field. If the insertion fails repeat. This functions just like the above but uses Mysql's native behavior to handle the checking so no user locking is necessary. Almost all the time this will require only 1 insertion whereas the above will require 2 or more accesses. 3) Generate a unique identifier from the record sequence number. Since the sequence number is guaranteed to be unique the identifier will also be unique and can be used without any worry. There are various schemes I can think of by which the unique identifier, altho uniquely determined by the sequence number, can be generated so that it will not reveal the sequence number and appear unpredictable. This will require 3 accesses to the DB (insertion, mysql_insert_id, and update). I have a number of options to try and think about. Thanks again. Best, Craig ------------------------------------------------------
-     Virtual Phonecards - Instant Pin by Email      -
-           Large Selection - Great Rates            -
-     http://speedypin.com/?aff=743&co_branded=1     -
------------------------------------------------------ 
**************************************
*                                    *
*          Craig Spencer             *
*      ccspencer@SoftHome.net        *
*                                    *
************************************** 


« previous php.general (#251207) next »