1

Good day, I just would like to ask if how can I generate random Alpha Numeric characters using PHP code / MySQL Code. I want to insert these values as unique Id for the user. Instead of using the AI in MySQL. . It may sound stupid, but Which is better (Approach)? Generating Unique Id, using Php/Sql programmatically?. . Range could be minimum of 20+ characters. E.g gHs51hJkw9106bwkdHwPV752

RoCkDevstack
  • 3,517
  • 7
  • 34
  • 56

2 Answers2

0

You can simpley use this function to get random alphanumeric string

function RandomString($length = 32) {
$randstr="";
srand((double) microtime(TRUE) * 1000000);
//our array add all letters and numbers if you wish
$chars = array(
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'p',
    'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5',
    '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 
    'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');

for ($rand = 0; $rand <= $length; $rand++) {
    $random = rand(0, count($chars) - 1);
    $randstr .= $chars[$random];
}
return $randstr;
}


//$length = 32 is used to set string length you can use whatever you want

I hope it will work for you

Yatin Khullar
  • 1,580
  • 1
  • 12
  • 26
0

You can use this code below . Its associated with the current timestamp so that its unique as you are planning to use it for AI .

<?php
$len =22;

$rand = substr(str_shuffle(md5(time())),0,$len);

echo $rand;

?>
Megan Fox
  • 435
  • 2
  • 6
  • 20