gpt4 book ai didi

php - 检查数据库中是否存在值,如果存在则返回其他值

转载 作者:行者123 更新时间:2023-11-29 11:22:30 27 4
gpt4 key购买 nike

我正在尝试生成随机代码,然后插入数据库我想生成其他随机代码(如果数据库中存在)

这是我的随机类(class):

    <?php
class code {
public $code;
public function mycode(){

$this->code = rand(1111111,9999999); //7894564

$this->code = str_replace(0,"A",$this->code);
$this->code = str_replace(1,"B",$this->code);
$this->code = str_replace(2,"-",$this->code);
$this->code = str_replace(3,"C",$this->code);
$this->code = str_replace(4,"I",$this->code);
$this->code = str_replace(5,"GB",$this->code);
$this->code = str_replace(6,"7",$this->code);
$this->code = str_replace(7,"W4",$this->code);
$this->code = str_replace(8,"S",$this->code);
$this->code = str_replace(9,"M",$this->code);
$this->code = str_replace(10,"RQ",$this->code);

$this->code = substr($this->code,0,9);

return $this->code;

}
}

$class_code = new code();
?>

在这里检查并打印代码

 $code = $class_code->mycode(); 
$get_code = mysqli_query($link,"Select * from links WHERE link = '".$code."'");
$if_exist = mysqli_num_rows($get_code);
if($if_exist == 1)
{
while($if_exist == 1)
{
// Here Return New Code
}
} else{
echo $code;
}

最佳答案

请检查下面的代码片段。您将能够使用它创建独特的随机代码。如果您有任何疑问,请告诉我。

<?php 
class code {
public $code;
public function mycode($link,$length=6){
//Generating random code
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$this->code = '';
for ($i = 0; $i < $length; $i++) {
$this->code .= $characters[rand(0, strlen($characters) - 1)];
}
//Check generated code is exist in Db or not
$get_code = mysqli_query($link,"Select * from links WHERE link = '".$this->code."'");
$if_exist = mysqli_num_rows($get_code);
if($if_exist == 1)
{
//if code is already exist then recursively call function to generate new unique code.
$this->mycode($link);
}
return $this->code;
}
}
//create object
$class_code = new code();
//call function to generate unique code.
//$link is the connection
$uniqueCode = $class_code->mycode($link);
?>

关于php - 检查数据库中是否存在值,如果存在则返回其他值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38682816/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com