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

时间:2013-08-19 15:11:37

标签: php

class Authorization {
    public $vk_id;
    public $eu_name;
    public $eu_society;
    public $eu_notes;
    public $eu_want_team;
    public $query;

    public function __construct() {
        $this->vk_id = $_POST['vk_id'];
        $this->eu_name = $_POST['eu_name'];
        $this->eu_society = $_POST['eu_society'];
        $this->eu_notes = $_POST['eu_notes'];
        $this->eu_want_team = $_POST['eu_want_team'];
    }

    function query($query) {
        $this->query = $query;
        $this->STH = $this->DBH->prepare($this->query);
        $this->STH->execute();
        $this->STH->setFetchMode(PDO::FETCH_ASSOC); 
    }
}
$auth = new Authorization();
$auth->query("INSERT INTO users (vk_id, eu_name, eu_society, eu_want_team, eu_notes) VALUES ($auth->vk_id, $auth->eu_name, $auth->eu_society, $auth->eu_want_team, $auth->eu_notes);");

它告诉 - :Call to a member function prepare() on a non-object line 21

line 21 is         $this->STH = $this->DBH->prepare($this->query);

那里有什么不好?

1 个答案:

答案 0 :(得分:0)

错误是因为您的类没有$DBH字段,因此它不存在,因此它不是对象。

可能你错过了在构造函数中实例化或注入$DBH值。