0

我在 php 上遇到一个恼人的错误说

Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\wamp\www\john\core\functions\users.php on line 16

Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\wamp\www\john\core\functions\users.php on line 24

第 16 行是指

return (mysql_result(mysql_query("Select 'id' From 'users' where 'username'= '$username'"), 0 ,'id'));

第 24 行是指

return (mysql_result(mysql_query("Select count('id') From 'users' where 'username'= '$username' AND 'hashed_password' = '$hash_password'"),0)==1 )? $user_id : false;

我的查询是正确的,但我认为查询有问题。请帮帮我。

整个代码在这里

 <?php 
function user_exists($username){
    $username = sanitize($username);
     return(mysql_result(mysql_query("SELECT COUNT(`id`) FROM `users` WHERE `username` = '$username'")
    , 0) == 1) ? true : false;
}

function user_active($username){
    $username = sanitize($username);
     return(mysql_result(mysql_query("SELECT COUNT(`id`) FROM `users` WHERE `username` = '$username' AND `active` = 1")
    , 0) == 1) ? true : false;
}

function user_id_from_username($username){
    $username = sanitize($username);
    return (mysql_result(mysql_query("Select 'id' From 'users' where 'username'= '$username'"), 0 ,'id'));
}


function login($username,$password){
    $user_id = user_id_from_username($username);
    $username = sanitize($username);
    $hash_password = md5($password);    
    return (mysql_result(mysql_query("Select count('id') From 'users' where 'username'= '$username' AND 'hashed_password' = '$hash_password'"),0)==1 )? $user_id : false;
}

?>
4

1 回答 1

4

问题在于您''在 column_names 周围使用。它被视为字符串而不是 column_name

改变

 return (mysql_result(mysql_query("Select 'id' From 'users' where 'username'= '$username'"), 0 ,'id'));

return (mysql_result(mysql_query("Select `id` From `users` where `username`= '$username'"), 0 ,'id'));

return (mysql_result(mysql_query("Select count(`id`) From `users` where `username`=
'$username' AND `hashed_password` = '$hash_password'"),0)==1 )? $user_id : false;
于 2013-03-30T05:37:20.273 回答