致命错误:对非对象PDO PHP类调用成员函数prepare()
因此,我正在编写一个名为article的PHP类,它有一系列方法,如insert、delete和update。我有3个其他函数遵循相同的代码,它们都可以工作,所以我不知道为什么会出现这个错误。这是我写的代码致命错误:对非对象PDO PHP类调用成员函数prepare(),php,mysql,pdo,Php,Mysql,Pdo,因此,我正在编写一个名为article的PHP类,它有一系列方法,如insert、delete和update。我有3个其他函数遵循相同的代码,它们都可以工作,所以我不知道为什么会出现这个错误。这是我写的代码 public function update(){ //make sure that the article does have an I.D if(is_null($this->id)) trigger_error("Article::update(): Attem
public function update(){
//make sure that the article does have an I.D
if(is_null($this->id)) trigger_error("Article::update(): Attempt to update an Article object that does not have its ID property set.", E_USER_ERROR );
//If there are any PDO errors they will be caught.
try{
$connection1 = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
$connection1->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$sqlQuery = "UPDATE articles SET lastEditDate=FROM_UNIXTIME(:lastEditDate), lastEditUser=:lastEditUser, title=:title, content=:content WHERE id = :id";
$statement->prepare($sqlQuery);
$statement->bindValue(":lastEditDate", $this->lastEditDate, PDO::PARAM_INT);
$statement->bindValue(":lastEditUser", $this->lastEditUser, PDO::PARAM_STR);
$statement->bindValue(":title", $this->title, PDO::PARAM_STR);
$statement->bindValue(":content", $this->content, PDO::PARAM_STR);
$statement->bindValue(":id", $this->id, PDO::PARAM_INT);
$statement->execute();
$connection1 = null;
}
catch(PDOException $e){
echo "update():I'm sorry, Dave. I'm afraid I can't do that.";
file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
}
}
我读到你们可以从以前的查询中得到它,所以我尝试重命名并将一堆变量设置为null。我还检查了我在这个网站和其他网站上找到的其他线程,几乎所有的线程都是范围问题,我认为这不是问题所在,因为我所有的其他函数都可以工作。我缺少了一些非常明显的东西吗?应该是
$connection1->prepare()
,而不是$statement->prepare()在哪里初始化$statement?我希望你不要在每个方法中都建立新的数据库连接…是的,我是迟钝的,应该是$statement=$connection->prepare($sqlQuery);