我在调用我的rand_id()
函数时遇到问题。这是代码
<?php
try
{
$config=array(
'DB_USERNAME'=>'root',
'DB_PASSWORD'=>'');
$conn=new PDO('mysql:host=localhost;dbname=scc',$config['DB_USERNAME'],$config['DB_PASSWORD']);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo rand_id();
}
catch(Exception $e)
{
echo 'error: '.$e->getMessage();
}
function rand_id()
{
$id=rand(100,103); //i have record with id ='100'
$results=$conn->query("select id from student_personal_info where id='".$id."'");
if($results->rowCount()>0)
{
rand_id();
}
else
{
return "this is unique id";
}
}
?>
但是如果我删除该功能并检查此代码工作正常但现在我在检查数据库后无法生成唯一的 id 消息......请帮助......
这是删除功能后的另一个代码
<?php
try
{
$config=array(
'DB_USERNAME'=>'root',
'DB_PASSWORD'=>'');
$conn=new PDO('mysql:host=localhost;dbname=scc',$config['DB_USERNAME'],$config['DB_PASSWORD']);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$id=rand(100,103); //i have record with id ='100'
$results=$conn->query("select id from student_personal_info where id='".$id."'");
if($results->rowCount()>0)
{
echo "generate again";
}
else
{
echo "this is unique id";
}
}
catch(Exception $e)
{
echo 'error: '.$e->getMessage();
}
?>