我试图创建一个PHP脚本,该脚本将创建一个随机代码并针对数据库进行检查以查看它是否存在。我希望检查是否存在,然后生成另一个createRandomCode()
并进行检查。
不确定如何继续循环,直到行数为0。
function createRandomCode($length='6'){
$chars = "abcdefghijkmnopqrstuvwxyz023456789";
srand((double)microtime()*1000000);
$i = 0;
$code= '';
while ($i++ < $length){
$code = $code. substr($chars, rand() % 33, 1);
}
return $code;
}
$shorturl = createRandomCode();
$q = $db - > query("SELECT * FROM maps WHERE url='".$shorturl.
"'");
if (mysqli_num_rows($q) > 0) {
$arr = json_encode($arr);
echo $arr;
}
答案 0 :(得分:4)
听起来像是do...while loop的好候选人:
headerTable.getDefaultCell().setPadding(1);
答案 1 :(得分:0)
function regenerateCode(){
global $connection;
do{
$serial2 = rand(0,7);
$check_code = "SELECT * FROM `voters` WHERE `serial` = '$serial2'";
$run_check_code = mysqli_query($connection,$check_code);
$count = mysqli_num_rows($run_check_code);
} while( $count > 0 );
return $serial2;
}