function GenerateID () { $len = 3; $base='ABCDEFGHKLMNOPQRSTWXYZ'; $max=strlen($base)-1; $activatecode=''; mt_srand((double)microtime()*1000000); while (strlen($activatecode)<$len+1)$activatecode.=$base{mt_rand(0,$max)}; $len = 7; $base='1234567890'; $max=strlen($base)-1; mt_srand((double)microtime()*1000000); while (strlen($activatecode)<$len+1) $activatecode.=$base{mt_rand(0,$max)}; return $activatecode; } this will generate and ID of 4 letters and 4 digits based on the current time stamp.. ex: *QCKS9046 ...* the possibility of generating the same ID again is almost 0... (and you can make it even less if you increase the length)... PS: I don't take credit for the code as it is a modified version of a code I found long time back (this same code is running on one of my applications for months without any problem).. Regards, Rabih G. Tayyem On 3/21/07, ccspencer@xxxxxxxxxxxx <ccspencer@xxxxxxxxxxxx> wrote:
Hello, I want to add a random unique ID to a Mysql table. Collisions are unlikely but possible so to handle those cases I'd like to regenerate the random ID until there is no collision and only then add my row. Any suggestions for a newbie as to the right way to go about doing this? Best, Craig ------------------------------------------------------ - Virtual Phonecards - Instant Pin by Email - - Large Selection - Great Rates - - http://speedypin.com/?aff=743&co_branded=1 - ------------------------------------------------------ ************************************** * * * Craig Spencer * * ccspencer@xxxxxxxxxxxx * * * ************************************** -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php