Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 类中有多个准备好的语句,获取错误:调用未定义的函数prepare()_Php_Mysql_Pdo - Fatal编程技术网

Php 类中有多个准备好的语句,获取错误:调用未定义的函数prepare()

Php 类中有多个准备好的语句,获取错误:调用未定义的函数prepare(),php,mysql,pdo,Php,Mysql,Pdo,我试图在我的“registration”类中运行以下查询,但得到了错误:调用未定义的函数prepare() 我似乎不正确地使用了事先准备好的陈述。这是我第一次尝试在类中使用它们,所以可能我遗漏了一些东西。我真的很想取出第二块代码并使用lastInsertId(),但我无法让它起作用,因此也欢迎任何与此相关的建议 try{ $sql = $this->$db->prepare("INSERT INTO `user_accounts` (`account_email`) VALU

我试图在我的“registration”类中运行以下查询,但得到了错误:
调用未定义的函数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());
}   

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

$sql_2 = $this->$db->prepare
你在追求什么

$sql_2 = $this->db->prepare

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

$sql_2 = $this->$db->prepare
你在追求什么

$sql_2 = $this->db->prepare

使用
$this->db
而不是
$this->$db

使用
$this->db
而不是
$this->$db

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

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


如果您没有将
$this->$db
更改为
$this->db
,它将不会工作。

当您发布时,我看到只有第二个prepare包含一个
$this->$db
,另外两个prepare正确使用了
$this->db
,但是现在,我看到您编辑了问题,并且所有问题都包含相同的错误
$this->$db

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


如果您没有将
$this->$db
更改为
$this->db
,它将不起作用。

这意味着
$this->db
未定义-检查您是否已正确初始化/注入类中<代码>$this->$db
是未定义的,当将$this与类的属性一起使用时,需要删除变量的$this…只是$this->dbI实际上不知道这一点。非常感谢你指出这一点!我可能早该知道,但我仍在学习如何使用课堂。那救了我一天!如果你愿意回答你的评论,我会给你评分。这意味着
$this->db
是未定义的-检查你是否正确初始化/注入了你的类<代码>$this->$db
是未定义的,当将$this与类的属性一起使用时,需要删除变量的$this…只是$this->dbI实际上不知道这一点。非常感谢你指出这一点!我可能早该知道,但我仍在学习如何使用课堂。那救了我一天!如果你想让你的评论成为一个答案,我会给你评分。通过在任何地方使用
$this->$db
修复了这个问题?我很抱歉,当你第一次给出答案时,我实际上误读了你的答案,然后转到@Smoot Q的评论,这让我知道了我做错了什么,所以我给了他评分。你说得对,我只是太迟钝了,没有意识到这一点。给你一票希望能弥补我的愚蠢。通过在任何地方使用
$this->$db
修复了这个问题?我很抱歉,当你第一次给出答案时,我实际上误解了你的答案,然后转到@Smoot Q的评论,这让我明白了我做错了什么,所以我给了他的答案。你说得对,我只是太迟钝了,没有意识到这一点。给你一票希望能弥补我的白痴。$sql\u 2=$this->db->prepare(“从
user\u accounts
中选择
user\u account\u id\u PK
WHERE
account\u email
=:email2”)$sql\u 2=$this->db->prepare(“从
user\u accounts
中选择
user\u accounts\u id\u PK
,其中
account\u email
=:email2”);是的,我误读了@Gzregorz-Oledzki的答案,在阅读你的评论并意识到我的错误之前,我把我的帖子编辑错了方向。我把这个搞糟了。好吧,请记住,当对类的属性使用$this时。。。您需要从变量中删除$。(例如:$this->x而不是$this->$x)。但是当将self或parent与类的变量(静态变量)一起使用时,您不会删除它(self:$x,parent:$x)是的,在阅读您的评论并意识到我的错误之前,我误读了@Gzregorz Oledzki的答案并以错误的方向编辑了我的帖子。我把这个搞糟了。好吧,请记住,当对类的属性使用$this时。。。您需要从变量中删除$。(例如:$this->x而不是$this->$x)。但是,当将self或parent与类的变量(静态变量)一起使用时,您不会删除它(self:$x,parent:$x)