获取错误:调用未定义的函数prepare()

时间:2014-01-02 11:20:32

标签: php mysql pdo

我正在尝试在“注册”课程中运行以下查询,而我收到错误: Call to undefined function prepare()

我似乎错误地使用预准备语句。这是我第一次尝试在课堂上使用它们,所以也许我在那里遗漏了一些东西。我真的想取出第二块代码并使用lastInsertId(),但我无法使用它,所以任何与此相关的建议都是受欢迎的。

try{
    $sql = $this->$db->prepare("INSERT INTO `user_accounts` (`account_email`) VALUES (:email);");
    $sql->bindValue(':email', $email);
    $sql->execute();
    //The following line is giving me the error.
    $sql_2 = $this->$db->prepare("SELECT `user_account_id_PK` FROM `user_accounts` WHERE `account_email` = :email2;");
    $sql_2->bindValue(':email2', $email);
    $sql_2->execute();
    $id = $sql_2->fetch(PDO::FETCH_ASSOC);

    $query  = $this->$db->prepare("INSERT INTO `users` (`username`, `password`, `email`, `ip`, `time`, `email_code`, `ua_id_FK`) VALUES (?, ?, ?, ?, ?, ?, ?) ");
    $query->bindValue(1, $username);
    $query->bindValue(2, $password);
    $query->bindValue(3, $email);
    $query->bindValue(4, $ip);
    $query->bindValue(5, $time);
    $query->bindValue(6, $email_code);
    $query->bindValue(7, $id);
    $query->execute();

    //mail($email, 'Please activate your account', "Hello " . $username. ",\r\nThank you for registering with us. Please visit the link below so we can activate your account:\r\n\r\nhttp://www.launchevergreen.com/activate.php?email=" . $email . "&email_code=" . $email_code . "\r\n\r\n-- Evergreen team");
} catch(PDOException $e) {
    die($e->getMessage());
}   

3 个答案:

答案 0 :(得分:4)

我不是PHP的专家,但可能不是......

$sql_2 = $this->$db->prepare

你在追求

$sql_2 = $this->db->prepare

答案 1 :(得分:2)

使用$this->db代替$this->$db

答案 2 :(得分:0)

当您发布时,我看到只有第二个准备包含$this->$db,其他两个正在使用$this->db,但是现在,我看到您编辑了问题并且所有这些都包含同样的错误$this->$db

所以我假设您在代码中添加了$ $this->db

如果您未将$this->$db更改为$this->db,则无效。

相关问题