当我将下面的函数添加到我的PHP页面时,它会超时尝试执行脚本。它有什么问题?
include ('config.php');
function GenerateTransID() {
$unique_ref_length = 9;
$unique_ref_found = false;
$possible_chars = "23456789BCDFGHJKMNPQRSTVWXYZ";
while (!$unique_ref_found) {
$unique_ref = "";
$i = 0;
while ($i < $unique_ref_length) {
$char = substr($possible_chars, mt_rand(0, strlen($possible_chars)-1), 1);
$unique_ref .= $char;
$i++;
}
$tmp_id = "TR-" . $unique_ref;
$unique_ref = $tmp_id;
$query = "SELECT `transactionID` FROM `payment` WHERE `transactionID`='$unique_ref'";
$result = mysql_query($query) or die(mysql_error().' '.$query);
if (mysql_num_rows($result)==0) {
$unique_ref_found = true;
}
}
return $unique_ref;
}
答案 0 :(得分:1)
该函数仅在mysql_num_rows($result) == 0
时返回,这可能在程序超时之前不会发生。
尝试添加一个echo,以查看它是否已执行。如果我是你,我会看一下生成GUID。
答案 1 :(得分:0)
更改为以下内容: -
function GenerateTransID() {
$unique_ref_length = 9;
$unique_ref_found = false;
$possible_chars = "23456789BCDFGHJKMNPQRSTVWXYZ";
while (!$unique_ref_found) {
$unique_ref = "";
for($i = 0; $i < $unique_ref_length; $i++) {
$char = substr($possible_chars, mt_rand(0, strlen($possible_chars)-1), 1);
$unique_ref .= $char;
}
$tmp_id = "TR-" . $unique_ref;
$unique_ref = $tmp_id;
$query = "SELECT `transactionID` FROM `payment` WHERE `transactionID`='$unique_ref'";
$result = mysql_query($query) or die(mysql_error().' '.$query);
if(mysql_num_rows($result)==0)
{
$unique_ref_found = true;
}
}
return $unique_ref;
}
进行了一些代码改进。除此之外,该功能应该按预期工作。您有另一个与此代码无关的问题,很可能是由于某些其他代码或您的数据库连接。
答案 2 :(得分:0)
可能是SQL查询。但是在你对它进行描述之前,你永远不会知道。检查xdebug: