Re: Re: Random Unique ID
| From: | Satyam | Date: | Fri, 23 Mar 2007 19:13:38 +0000 |
| Subject: | Re: Re: Random Unique ID | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-251213@lists.php.net to get a copy of this message | ||
I'm sorry if I am repeating something that has been said, but in the comments to:
http://es2.php.net/manual/en/function.uniqid.php
there is a little piece of code that generates UUID as per RFC 4122, and those get as unique as they come. MySql has a UUID() function and so do any database engine that supports replication, since that's the only way to ensure keys won't get repeated over multiple servers.
Satyam
----- Original Message ----- From: <ccspencer@softhome.net>
To: <php-general@lists.php.net>
Sent: Friday, March 23, 2007 7:48 PM
Subject: [PHP] Re: Random Unique ID
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 * * *************************************** -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- No virus found in this incoming message. Checked by AVG Free Edition. Version: 7.5.446 / Virus Database: 268.18.17/730 - Release Date: 22/03/2007 7:44