如果查询失败,我需要返回false ...我把它放在哪里......
function asas($username,$password){
$qry = "SELECT *
FROM members
WHERE login='$username' AND passwd='".md5($password)."'";
$result = mysql_query($qry);
while($row = mysql_fetch_array($result))
{
return(array($row['userlevel'], $row['email']));
}
//return 'Invalid username / password ';
}
答案 0 :(得分:3)
mysql_query 失败时返回false,就像mysql_fetch_array一样。所以你的代码应该如下:
function asas($username,$password) {
$qry="SELECT *
FROM members
WHERE login='$username' AND passwd='".md5($password)."'";
$result=mysql_query($qry);
// if fail is intended the failure of the mysql connection
if (!$result) return false;
$row = mysql_fetch_array($result);
if ($row) return array($row['userlevel'],$row['email']);
// if fail is intended as username and password didn't match
return false;
}
您会注意到该函数有几个退出点。如果是家庭作业,你应该把它们集中在一个。
答案 1 :(得分:1)
if(mysql_num_rows($result)===0)
return false;
else
while(...
我是如何看待它的。您甚至可以在while循环后直接使用return false;
。尝试一下,看看它有效。
答案 2 :(得分:1)
function asas($username,$password){
$qry="SELECT * FROM members
WHERE login='$username' AND passwd='".md5($password)."'";
$result=mysql_query($qry);
if (!$result) {
/* Something went wrong with the query */
return false;
}
while($row = mysql_fetch_array($result))
{
return(array($row['userlevel'],$row['email']));
}
/* if the query found any rows then the code above would or returned it */
return false;
//return 'Invalid username / password ';
}