在非对象上调用成员函数escape_string()

时间:2014-09-10 20:49:37

标签: php mysql

我收到以下错误:

Fatal error: Call to a member function escape_string() on a non-object

关于代码:

$pass = $_GET['pass'];
$user = $_GET['user'];
//$id = $_GET['uid'];   
echo "u: ".$user." pass: ".$pass;
echo "</br>TEST";
//$check = validate_password_from_uid($id, $pass);
$check = validate_password_from_username($user, $pass);
echo "validation: </br>";
if (is_array($check)){
echo "<pre>";
    print_r($check);
    echo "</pre>";
}else{
    var_dump($check);
}

以下是validate_password_from_username

function validate_password_from_username($username, $password)
{
    global $db, $mybb;

    $username = $db->escape_string(strtolower($username));
    switch($mybb->settings['username_method'])
    {
        case 0:
            $query = $db->simple_select("users", "uid,username,password,salt,loginkey,coppauser,usergroup", "LOWER(username)='".$username."'", array('limit' => 1));
            break;
        case 1:
            $query = $db->simple_select("users", "uid,username,password,salt,loginkey,coppauser,usergroup", "LOWER(email)='".$username."'", array('limit' => 1));
            break;
        case 2:
            $query = $db->simple_select("users", "uid,username,password,salt,loginkey,coppauser,usergroup", "LOWER(username)='".$username."' OR LOWER(email)='".$username."'", array('limit' => 1));
            break;
        default:
            $query = $db->simple_select("users", "uid,username,password,salt,loginkey,coppauser,usergroup", "LOWER(username)='".$username."'", array('limit' => 1));
            break;
    }

    $user = $db->fetch_array($query);
    if(!$user['uid'])
    {
        return false;
    }
    else
    {
        return validate_password_from_uid($user['uid'], $password, $user);
    }
}

该错误与$db->escape_string部分所在的第67行有关。我应该改变什么?

1 个答案:

答案 0 :(得分:1)

错误消息告诉我们$ db变量不是对象,因此您无法对其进行函数调用。您没有向我们展示如何定义$ db,因此您可能以不同方式命名变量。