为什么我会收到致命错误?

时间:2013-11-02 20:57:36

标签: php sql database login

以下是导致错误的代码段。

public function storeUser($email, $password) {

    require_once 'include/user.config.inc.php';

    $uuid = uniqid('', true);
    //echo $uuid;
    $hash = $this->hashSSHA($password);
    //echo $hash;
    $encrypted_password = $hash["encrypted"]; // encrypted password
    //echo $encrypted_password;
    $salt = $hash["salt"]; // salt
    //echo $salt;


    $query = "INSERT INTO user_table (unique_id, email_id, encrypted_password, salt, created_at) VALUES ( :uuid, :email, :encrypted_password, :salt, NOW()) ";
    $query_params = array(
        ':uuid' => $uuid,
        ':email' => $email,
        ':encrypted_password' => $encrypted_password,
        ':salt' => $salt
    );
    try {

        //These two statements run the query against the database table.
        $stmt = $db -> prepare($query);
        $result = $stmt -> execute($query_params);

    } catch (PDOException $ex) {

        $response["success"] = 0;
        $response["message"] = "Database Error! Problem with first query!";
        die(json_encode($response));

    }

}

我收到错误:

注意:未定义的变量:db

致命错误:在非对象上调用成员函数prepare()

似乎$ query和$ query_params导致问题,但我不知道为什么。对我来说似乎是对的。

这是另一段代码

if ($db->isUserExisted($email)) {

             // user is already existed - error response
            $response["error"] = 2;
            $response["error_msg"] = "User already exist";
            echo json_encode($response);

        } else {

            $db->storeUser($email, $password);

        }

2 个答案:

答案 0 :(得分:1)

在这里猜测......

require_once只包含文件 一次 ,*。你可能在其他地方之前有required个文件,它没有再次加载,你的$db变量无处可寻。

*在当前脚本执行中。

答案 1 :(得分:0)

您永远不会初始化$ db对象。

在“$ stmt = $ db - > prepare($ query);”之前需要类似以下内容;

$db = new mydbclass();