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

时间:2014-06-28 03:37:27

标签: php mysql database pdo connection

我正在尝试使用PDO从数据库中获取数据,但我一直收到错误" 致命错误:在非对象上调用成员函数prepare()&#34 ;。 我到处搜索过但未能找到解决这个问题的方法。以下是我的完整代码...

class StatusPoster{
    private $db = NULL;

    const DB_SERVER = "localhost";
    const DB_USER = "root";
    const DB_PASSWORD = "";
    const DB_NAME = "status_poster";

    public function _construct(){
        $dsn = 'mysql:host='.self::DB_SERVER.';dbname='.self::DB_NAME;

        try{
            $this->db = new PDO($dsn, self::DB_USER, self::DB_PASSWORD);
        }
        catch(PDOexception $e){
            throw new Exception("Connection failed!!! ".$e->getMessage());
        }
        return $this->db;

    }

    public function getStatusPosts(){
                    $query = "SELECT name,image,status,timestamp FROM status ORDER BY DESC, id";
        $statement = $this->db->prepare($query);
        $statement->execute();

        if($statement->rowCount() > 0){
            return $statement->fetchAll();
        }

        return false;
    }
}


任何帮助将不胜感激 谢谢

0 个答案:

没有答案