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
Asked
Active
Viewed 73 times
1
-
4try this link http://stackoverflow.com/questions/4356289/php-random-string-generator – Denis Bhojvani May 18 '16 at 10:59
-
http://stackoverflow.com/questions/7480489/php-random-string-generator-without-repeats – JYoThI May 18 '16 at 11:26
2 Answers
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
-
Sir can you explain to me the time thing? Will it create id depends on your time format? – RoCkDevstack May 18 '16 at 11:20
-
time function generates the current Unix timestamp. which is unique each and everytime . Yes it will generate a unique random string so that you use it as your Primary key – Megan Fox May 18 '16 at 11:25
-
This intrigues me, I didn't found/read this answer so far. Many thank you. – RoCkDevstack May 18 '16 at 11:32