0

Im a newbie to php trying understand why i get boolean error in my specific case with a code that works for others. I have a function that returns a $user_id which is to be used in a session later on. Here is he function:

    function login($username, $password) {
       $user_id = user_id_from_username($username);
       $username = sanitize($username);
       $password = sha1($password);
       return (mysql_result(mysql_query("SELECT COUNT (user_id) FROM users WHERE username = '$username' AND password = '$password'"), 0) == 1) ? $user_id : false ;
   } 

This always results in a fail, with this error: Warning: mysql_result() expects parameter 1 to be resource, boolean given in. with a line number that points to this line.

  return (mysql_result(mysql_query("SELECT COUNT (`user_id`) FROM users WHERE username      = '$username' AND password = '$password'"), 0) == 1) ? $user_id : false ;

Why does this fail ? Isn't this enough to check weather the query was successful and test result. DB connection and sql query seemed to be correct, doesn't matter whether i use ' or omit. After several hours of research i managed to remove the error with an if statement. Like so :

    function login($username, $password) {
       $user_id = user_id_from_username( $username );
       $username = sanitize( $username );
       $password = sha1( $password );
       $result = mysql_query("SELECT COUNT (user_id) FROM users WHERE username = '$username'AND password = '$password'");
       if ( $result == 1 ) {
           return $user_id;
       } else if ( $result == 0 ) {
           return false;
       }
   }

I would like to know why the second function works and not the first. Do you have to use an if statement to check the query ? If there is a better way to write this function please suggest.:)

1
  • Update: I was struggling with this code all night day long cause it would not match the password and username even though username and password are correct. Found out that leaving out sha1 in $password, does the job and matches. Why does the password security cause the function to return false ?? Weird. Commented Mar 25, 2013 at 23:15

1 Answer 1

1

Probably you have error in query so mysql_query return false

Replace:

return (mysql_result(mysql_query("SELECT COUNT (user_id) FROM users WHERE username = '$username' AND password = '$password'"), 0) == 1) ? $user_id : false ;

with:

$result = mysql_query("SELECT COUNT(user_id) as count FROM users WHERE username = '$username' AND password = '$password'") or die(mysql_error());

return (mysql_result($result, 0, 'count') == 1) ? $user_id : false ;

and you will see mysql error.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Narek your code did work, no error :). Just so i understood this, so i have to separate mysql_query and and mysql_result and using "as count" for user_id is also necessary. Can that be applied to this code as well. to this code as well. function user_exists($username) { $username = sanitize($username); return (mysql_result(mysql_query("SELECT COUNT(user_id) FROM users WHERE username = '$username'"), 0) == 1) ? true : false;}} – IF so how , i did tried to apply it your solutaion here this but it failed:
$result = mysql_query("SELECT COUNT (username) as count FROM users WHERE username = '$username'") or die("Could not perform select query - " . mysql_error());;; return (mysql_result($result, 0, 'count') === 1) ? true : false That results in Could not perform select query - FUNCTION lr.COUNT does not exist. Check the 'Function Name Parsing and Resolution' section in the Reference Manual. What am i doing wrong again ? Note: I dont manage to make code block , when i press enter it posts the text sorry.
Change COUNT (username) to COUNT(username) (without space), but better to COUNT(*), will work faster.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.