내 rand_id()
함수를 호출하는 데 문제가 있습니다. 여기에 ......사용자 정의 함수가 PHP에서 작동하지 않습니다
<?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 메시지를 생성 할 수없는 경우 ... PLZ 도움이
여기 $conn
가 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);
$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();
}
?>
구체적으로 문제 또는 오류가 무엇입니까?! – deceze
의미있는 들여 쓰기로 코드를 다시 포맷 하시겠습니까? –
쿼리에서 함수를 분리하고 값을 반환하여 함수 밖에서 검사하도록하십시오. –