检查表中是否存在并生成随机字符串的循环

时间:2019-04-01 17:57:43

标签: php mysql sql loops mysqli

我有一个生成随机字符串的函数

    $found = true;
function generateRandomString($length = 10) {
$str = substr(str_shuffle(str_repeat($x='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length/strlen($x)) )),1,$length);
 while($found == true)
{
     $check1 = $baza->query("SELECT key_X FROM generatedkey WHERE generatekey = '$str'");;
    if(mysql_num_rows($check1)==0) {
        $found = false;
    } else {

       $str = generateRandomString();

    }
}
return $str;
$found = true;
}

我想知道它是否正确,因为我不知道如何检查。 此功能应如下所示:

  • 生成随机字符串
  • 检查表中是否存在字符串
  • 如果存在(>创建新的内容)
  • 如果不存在

  • 返回随机字符串

1 个答案:

答案 0 :(得分:0)

您的代码有几个问题:

$found = true; //defined outside the function

function generateRandomString($length = 10) {
     //string is generated outside the loop
    $str = substr(str_shuffle(str_repeat($x='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length/strlen($x)) )),1,$length);

    while($found == true) //found is undefined so this never runs
    {
         $check1 = $baza->query("SELECT key_X FROM generatedkey WHERE generatekey = '$str'");;
        if(mysql_num_rows($check1)==0) {
            $found = false;
        } else {
           $str = generateRandomString(); //recursion or a loop not both
        }
    }
    return $str;
    $found = true; //pointless
}

循环播放

function generateRandomString($length = 10) {
    while(true)
    {
        $str = substr(str_shuffle(str_repeat($x='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length/strlen($x)) )),1,$length);
        $check1 = $baza->query("SELECT key_X FROM generatedkey WHERE generatekey = '$str'");


        if(mysql_num_rows($check1)==0) break; //no results=unique, so bail out of the loop
    }
    return $str;
}

具有递归

function generateRandomString($length = 10) {

    $str = substr(str_shuffle(str_repeat($x='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length/strlen($x)) )),1,$length);
    $check1 = $baza->query("SELECT key_X FROM generatedkey WHERE generatekey = '$str'");
    if(mysql_num_rows($check1)!=0) $str=generateRandomString($length); //results=not unique, do recursion

    return $str;
}

PS。我将准备好的语句放到这里,因为它只是一个随机字符串,没有用户输入。

个人

我认为使用循环对此很合适,我唯一要添加的就是如果迭代次数过多,则会引发异常。这只是其中之一,永远都不会发生。

要考虑的一件事是10确实没有那么长。最近,我们在证书代码上的失败率约为2%(8个长)。我唯一能想到的是它们是非唯一碰撞(这是第三方LMS)。这个系统大约有6年的历史了,我正在努力更换它。因此,根据生成这些频率的频率,它们不是无限的,并且随着时间的推移,发生碰撞的机会会增加。

function generateRandomString($length = 10, $max_iterations=100) {
    for($i=0;$i<$max_iterations;++$i){
        $str = substr(str_shuffle(str_repeat($x='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length/strlen($x)) )),1,$length);
        $check1 = $baza->query("SELECT key_X FROM generatedkey WHERE generatekey = '$str'");

        if(mysql_num_rows($check1)==0) return $str; //return when condition met
    }
    //if were here then we didn't return $str above
    throw new Exception("too many iterations");
}