RE: [PHP-DB] generating unique id's

From: Date: Fri, 30 Jun 2000 16:00:21 +0000
Subject: RE: [PHP-DB] generating unique id's
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-793@lists.php.net to get a copy of this message
If you're really paranoid about possible duplicate values, you can use your database to store one seed value. Then each time you need to get a unique identifier, you lock the table with the seed, pull it out, use it to generate a new random number, then put that number back in as the seed and unlock the table. You can use the random number to generate your unique identifier (string or number). That way even if you're running a site on multiple Web servers and people request IDs at the same instant, they won't get the same IDs. Of course this is a hit to the database, and you lock up a table, but that may be worth it to you if it's important to have truly unique ID values. Vicky -----Original Message----- From: Edward Wong [mailto:wyuanzheng@21cn.com] Sent: Tuesday, June 29, 1999 7:14 PM To: Cody Caughlan; php-db@lists.php.net Subject: Re: [PHP-DB] generating unique id's Hi: I use the following code: <? for ($i = 0; $i < 5; $i++) { mt_srand((double)microtime()*1000000); $randval[$i] = mt_rand(); } for ($i = 0; $i < 5; $i++) { echo $randval[$i],"<BR>"; } ?> or <? // Method 2 mt_srand((double)microtime()*1000000); ECHO "<FONT COLOR=RED>"; for ($i = 0; $i < 5; $i++) { $randval[$i] = mt_rand(); } for ($i = 0; $i < 5; $i++) { echo $randval[$i],"<BR>"; } ECHO "</FONT>"; ?> and the result is: 122653537 849966493 749620674 706811529 86123690 2074739843 1867927643 1706046782 1795463355 1221802180 Any one can meet your needs, you see. If you can't, maybe your computer is too good. :-) ------------------------------------- # Edward Wong # # # # E-Mail:wyuanzheng@21cn.com # # Phone: 86-21-63615261 * 207 # # # # iSpaceBank Group, Shanghai, China # # # ------------------------------------- ----- Original Message ----- From: "Cody Caughlan" <codyc@cisco.com> To: <php-db@lists.php.net> Sent: Thursday, June 29, 2000 6:10 AM Subject: [PHP-DB] generating unique id's > Hello, > > I need to generate a unique id. The problem I am running into is this: > I have a for loop that generates a bunch of id's using the time() > function, the problem is that since the for loop only takes less than a > microsecond, and the precision of the time() function is a microsecond, > all the id's are the same. I looked into using uniqid(), which according > to the PHP manual generates an INT. However, I tested it out, and in a > microsecond it DOES generate a series of different, random STRINGS, mix of > ints and characters, NOT an INT, which like I said is what the PHP manual > uses. MySQL requires that the primary key be an INT, so I can use uniqid() > function to generate this id, because its a string. > > What can I do? > > In summary, generate a whole list of unique INTEGER id's in under a > microsecond... > > Thanks > /Cody Caughlan > > > -- > PHP Database Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-db-unsubscribe@lists.php.net > For additional commands, e-mail: php-db-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.db (#793) next »