blueboy ha scritto:
Hi,

I want to create random customer ids. I have an auto incremented coulumn but I would rather have a 6-8 digit/letter id randomly generated that can be used as a unique identifier across 3 tables. Does anyone have a algorithm to generate such a string and can you give the odds against 2 duplicate stings being generated?

I know this is a strange ask.


R.

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);

Cheers,

Burn

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

Reply via email to