I want to create a specific-sized random "key" for every row in the database table. For example, for the "id" or row (auto-increment ID) "20", the column "key" will have "xEd456" and for row 21, it will be e.g. "rYU875". However, I also want to make sure there is no way there is a duplicate "key" in the table. I prefer to have 6-char "key". How do I achieve this?
That means you need a alphanumeric key, see the code
while(true)
{
$str="";
for($i=0;$i<6;$i++)
{
$rnd=rand(1,16);
if(intval(fmod($rnd, 2)))
$a=rand(48,57);
else
$a=rand(65,90);
$str .= chr($a);
}
// skipping the mysql connection functions
$sql=mysql_query(“select user_id from user_master_table where user_id_alias='".$str."'");
$rs=mysql_fetch_array($sql);
if($rs["user_id"]=="")
break;
}
You are free to change the loop value and random number range.