我有以下递归函数,如果必须循环一次,它将不会返回任何内容。我可以在返回条件中回显用户名,并在循环后显示正确的用户名,附加一个数字(例如jdoe_45),但它不返回它。
function createUsername ($conn, $firstname, $lastname, $numerate = false) {
$fn = preg_replace("/[^A-Za-z0-9 ]/", '', $firstname);
$ln = preg_replace("/[^A-Za-z0-9 ]/", '', $lastname);
$arg = ($numerate) ? '_' . rand(11, 99) : '';
$username = strtolower(substr($fn, 0, 1) . $ln . $arg);
$sql = "SELECT * FROM users WHERE username = '$username'";
$rs = mysql_query($sql, $conn) or die ("Could not execute query.");
$num = mysql_numrows($rs);
if ($num == 0) {
return $username;
}
createUsername($conn, $firstname, $lastname, 1);
}
答案 0 :(得分:0)
您忘了取结果:
if ($num == 0) {
$row = mysql_fetch($rs);
$username = $row['username'];
return $username;
}
答案 1 :(得分:0)
您还需要返回递归调用:
return createUsername($conn, $firstname, $lastname, 1);
目前,您只能在$num == 0
时返回。