【问题标题】:Why am i getting a fatal error? [duplicate]为什么我会遇到致命错误? [复制]
【发布时间】:2013-11-02 20:57:35
【问题描述】:

这是导致错误的代码的 sn-p。

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 导致了问题,但我不知道为什么。在我看来是对的。

这是另一个sn-p代码

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);

        }

【问题讨论】:

  • $db 在哪里定义? PHP 说它丢失了,因此失败了...
  • 它在include/user.config.inc.php中初始化

标签: php sql database login


【解决方案1】:

在这里猜一猜……

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

* 在当前脚本执行中。

【讨论】:

  • 是的,这是问题所在,但现在我收到此错误“会话已开始 - 忽略 session_start()”我如何结束上一个会话,因为我有两个函数分别访问数据库场合。我添加了调用这两个函数的代码
【解决方案2】:

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

在 "$stmt = $db -> prepare($query);" 之前你需要类似下面的东西

$db = new mydbclass();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-26
    • 1970-01-01
    • 2013-11-27
    • 2021-07-12
    • 2020-07-16
    • 1970-01-01
    • 2021-10-19
    相关资源
    最近更新 更多