您没有说明为什么是not working
,但我的猜测是,在//insert into events table
下面的代码中,您正在使用$newEventId
in INSERT
,并且如果最初的$newEventId
原因(mysql_num_rows(mysql_query("SELECT id FROM events WHERE EventID=$gen" )) == 0)
返回它,它工作正常true
,但如果它返回true
递归函数则不是。
尝试返回值break
并使用它而不是$newEventId
function EventId($gen)
{
if (mysql_num_rows(mysql_query("SELECT id FROM events WHERE EventID=$gen" )) == 0) {
return $gen; // return the number that will be set as $EventId and used in the insert
break;
}
// recall function
else
{
$newEventId = rand(1000, 10000);
EventId($newEventId);
}
}
$newEventId = rand(1000, 10000);
$EventId = EventId($newEventId); // set the returned $gen to $EventId to be used in the Insert query
//insert into events table
mysql_query("INSERT INTO events ... EventID=$EventId..");