在PHP中失败的条件下返回值

时间:2022-09-10 23:26:41

I need a return false if the query fails... where do i put it...

如果查询失败,我需要返回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 ';
}

3 个解决方案

#1


mysql_query return false on fail, just like mysql_fetch_array. so your code should be like the following:

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; 
}

You will notice that the function has several exit points. In case of homework you should concentrate them in a single one.

您会注意到该函数有几个退出点。如果是家庭作业,你应该把它们集中在一个。

#2


if(mysql_num_rows($result)===0)
    return false;
else
    while(...

Is how I generally see it written. You might even be able to simply return false; directly after the while loop. Try it out, see what works.

我是如何通常看到它写的。你甚至可以简单地返回false;直接在while循环之后。尝试一下,看看它有效。

#3


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 ';
}

#1


mysql_query return false on fail, just like mysql_fetch_array. so your code should be like the following:

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; 
}

You will notice that the function has several exit points. In case of homework you should concentrate them in a single one.

您会注意到该函数有几个退出点。如果是家庭作业,你应该把它们集中在一个。

#2


if(mysql_num_rows($result)===0)
    return false;
else
    while(...

Is how I generally see it written. You might even be able to simply return false; directly after the while loop. Try it out, see what works.

我是如何通常看到它写的。你甚至可以简单地返回false;直接在while循环之后。尝试一下,看看它有效。

#3


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 ';
}