tags:

views:

81

answers:

3

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

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 ';
}
+1  A: 
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.

Simon Scarfe
+3  A: 

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

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.

Eineki
+1  A: 
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 ';
}
Andi McLean
I edited your answer as the code wasn't indented, and thus not formatted when displayed. You can use the code formatting button in the editor to do this (the one with binary digits on it). Welcome to stack overflow :)
Paul Dixon