Php 当存在时,请再次运行

Php 当存在时,请再次运行,php,mysql,while-loop,Php,Mysql,While Loop,我有两个PHP函数,一个用于生成5个字符的随机字符串,另一个用于检查MySQL数据库中是否存在随机字符串。现在我想运行函数ShortURL(),直到函数CheckShortURL()返回FALSE,然后继续执行脚本。非常感谢 function ShortURL() { $Length = "5"; $RandomString = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHI

我有两个PHP函数,一个用于生成5个字符的随机字符串,另一个用于检查MySQL数据库中是否存在随机字符串。现在我想运行函数
ShortURL()
,直到函数
CheckShortURL()
返回FALSE,然后继续执行脚本。非常感谢

     function ShortURL() {
       $Length = "5";
       $RandomString = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $Length);
       return $RandomString;
     }

     function CheckShortURL($RandomString) {
       try {
         $SQL = "SELECT ShortURL FROM Images WHERE ShortURL = :ShortURL";
         $SQL = $CONN->prepare($SQL);
         $SQL->execute(array("ShortURL" => $ShortURL));
         $CountShortURL = $SQL->rowCount();
       } catch(PDOException $e) {
         echo "Error: " . $e->getMessage();
         exit();
       }
       if($CountShortURL == "1") {
         return TRUE;
       } else {
         return FALSE;
       }
     }
更新:

我可以使用此代码在循环时执行
,直到得到唯一的5个字符:

     $ShortURL = ShortURL();

     while(CheckShortURL($ShortURL, $CONN) == TRUE) {
       $ShortURL = ShortURL();
     }
更新2:

     $ShortURL_Found = "0";

     while(!$ShortURL_Found) {
       $ShortURL = ShortURL();
       if(CheckShortURL($ShortURL, $CONN) == FALSE) {
         $ShortURL_Found = "1";
       }
     }

快速伪代码while循环示例:

<?php 

  ...

  $bFound = 0; 

  while(!$bFound) {
      1. create random string
      2. look in db, is that string there? If so, set $bFound=1
      (that's it, just fall down to below squiggly brace in next line)
  }

  // yeehaw, I am out of loop

  ...

你不知道
循环?或者你的问题是关于什么的?一个带while循环的函数怎么样。题目很精确问题是我不知道如何做
while
循环,我从PHP开始,我无法理解教程中的循环。我更新了循环,我认为它可以很好地工作。你也可以使用do-while循环。结果相同,但阅读能力更好
do{something}while(condition)
我会使用int而不是字符串作为布尔值,如我所示。测试它,至少您正在做准备、PDO和异常,非常好!成功和失败测试中的硬代码,也是启动新语言时调试过程的一部分。我已更改
while(!$ShortURL\u Found)
while($ShortURL\u Found==“0”)