Re: Re: customer ids

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Burn,

Wednesday, June 6, 2007, 3:55:21 PM, you wrote:

> Here's how I do it, for 1 single table though.. you'll have to write
> yourself the mod to check on more tables. If you plan to have > 10 
> millions records make sure the maxrand is higher.

> When the "do" loop exits you have a unique id.

> function generateID() {
>         $minrand = 1;
>         $maxrand = 99999999;
>         $uniqueid[0] = mt_rand($minrand, $maxrand);
>         $uniqueid[1] = $uniqueid[0];
>         return $uniqueid[1];
> }
>         
> do {
>         //New ID generation
>         $newid = generateID();
>         //Database check
>         $sqlcheck = "SELECT youruniqueidhere FROM yourtablehere WHERE 
> youruniqueidhere = '".$newid."'";
>         $conn = mysql_connect($db_host, $db_username, $db_password);
>         mysql_select_db($db_name, $conn);
>                 
>         $rs = mysql_query($sqlcheck, $conn);
>         $rscount = mysql_num_rows($rs);
> } while ($rscount != 0);

Not that it is likely to occur on a low-traffic site, but the above
approach is a race condition waiting to happen I'm afraid. It will
however almost certainly satisfy the needs of the OP as long as his
site stays off digg / slashdot :)

Cheers,

Rich
-- 
Zend Certified Engineer
http://www.corephp.co.uk

"Never trust a computer you can't throw out of a window"

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux