Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Php/SQL中生成字母数字,哪种方法更好?_Php_Mysql - Fatal编程技术网

如何在Php/SQL中生成字母数字,哪种方法更好?

如何在Php/SQL中生成字母数字,哪种方法更好?,php,mysql,Php,Mysql,您好,我只是想问一下如何使用PHP代码/MySQL代码生成随机字母数字字符。我想将这些值作为用户的唯一Id插入。而不是在MySQL中使用AI。 . 这听起来可能很愚蠢,但哪种方法更好? 以编程方式使用Php/Sql?生成唯一Id?。 . 范围可以至少为20个以上字符。例如GHS51HJKW9106BKDHWPV752您可以简单地使用此函数获取随机字母数字字符串 function RandomString($length = 32) { $randstr=""; srand((double) mi

您好,我只是想问一下如何使用PHP代码/MySQL代码生成随机字母数字字符。我想将这些值作为用户的唯一Id插入。而不是在MySQL中使用AI。 . 这听起来可能很愚蠢,但哪种方法更好? 以编程方式使用Php/Sql?生成唯一Id?。 .
范围可以至少为20个以上字符。例如GHS51HJKW9106BKDHWPV752

您可以简单地使用此函数获取随机字母数字字符串

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
函数随机字符串($length=32){
$randstr=“”;
srand((双)微时(真)*1000000);
//如果您愿意,我们的数组可以添加所有字母和数字
$chars=数组(
‘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’;

对于($rand=0;$rand,您可以简单地使用此函数获取随机字母数字字符串

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
函数随机字符串($length=32){
$randstr=“”;
srand((双)微时(真)*1000000);
//如果您愿意,我们的数组可以添加所有字母和数字
$chars=数组(
‘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’;

对于($rand=0;$rand您可以使用下面的代码。它与当前时间戳关联,因此在您计划将其用于AI时,它是唯一的

<?php
$len =22;

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

echo $rand;

?>

您可以在下面使用此代码。它与当前时间戳关联,因此在您计划将其用于AI时,它是唯一的

<?php
$len =22;

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

echo $rand;

?>


试试这个链接试试这个链接先生,你能给我解释一下时间吗?它会创建id取决于你的时间格式吗?时间函数会生成当前的Unix时间戳。它每次都是唯一的。是的,它会生成一个唯一的随机字符串,以便你把它作为主键。这让我很感兴趣,我没有找到/阅读这个答案,所以far.非常感谢。先生,你能给我解释一下时间吗?它会创建id取决于你的时间格式吗?时间函数会生成当前的Unix时间戳。它每次都是唯一的。是的,它会生成一个唯一的随机字符串,以便你用它作为主键。这让我很感兴趣,到目前为止我还没有找到/阅读这个答案。非常感谢欧点。