与pdo文件连接失败

时间:2016-01-03 19:08:12

标签: php mysql pdo

我尝试使用PDO类对项目进行编码,但在出错时失败

  

“在非对象上调用成员函数prepare()”

我在stackoverflow上找到了一些答案,但它们没有用,我现在不知道该怎么做。我开始尝试修复这个2天前,我仍然无法解决这个问题。如果你知道问题出在哪里,请帮助我。

我的配置文件:     

//DB Params
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASS", "usbw");
define("DB_NAME", "talkingspace");

//define("SITE_TITLE", "Welcome to Talking Space");

//Paths
define('BASE_URI', 'http://'.$_SERVER['SERVER_NAME'].'/MVC/');

?>

我的PDO文件

<?php class Database {
    private $host = DB_HOST;
    private $user = DB_USER;
    private $pass = DB_PASS;
    private $dbname = DB_NAME;

    private $dbh;
    private $error;
    private $stmt;

    public function __construct() {
        //Set DSN
        $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
        //Set options
        $options = array(
            PDO::ATTR_PERSISTENT => true,  
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        );
        //Create a new PDO instance
        try {
            $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
        } catch (Exception $e) {
            $this->error = $e->getMessage();
        }
    }      
    public function query($query) {
        $this->stmt = $this->dbh->prepare($query);
    }  
    public function bind($param, $value, $type=null) {
        if (is_null($type)) {
            switch (true) {
                case is_int($value):
                    $type = PDO::PARAM_INT;
                    break;
                case is_bool($value):
                    $type = PDO::PARAM_BOOL;
                    break;
                case is_null($value):
                    $type = PDO::PARAM_NULL;
                    break;
                default:
                    $type = PDO::PARAM_STR;            
            }
        }
        $this->stmt->bindValue($param, $value, $type);
    }    
    public function execute() {
        return $this->stmt->execute();
    }  
    public function resultset() {
        $this->execute();
        return $this->stmt->fetchAll(PDO::FETCH_OBJ);
    }
    public function single() {
        $this->execute();
        return $this->stmt->fetch(PDO::FETCH_OBJ);
    }
    public function rowCount() {
        return $this->stmt->rowCount();
    }    
    public function lastInsertId() {
        return $this->dbh->lastInsertId();
    }  
    public function beginTransaction() {
        return $this->dbh->beginTransaction();
    }      
    public function endTransaction() {
        return $this->dbh->commit();
    }
    public function cancelTransaction() {
        return $this->dbh->rollBack();
    }
}

1 个答案:

答案 0 :(得分:0)

  

错误失败“在非对象上调用成员函数prepare()”

这是因为您在single()方法之前调用了resultset()query()方法。语句的正确顺序应该是这样的:

$db = new Database();
$db->query("SELECT * FROM table_name");
$result = $db->single();  // or $result = $db->resultset();
var_dump($result); // to see the result set

<强>编辑:

name是MySQL中的保留关键字。你应该用反引号逃脱它。始终使用反引号来包装表名和列名,并使用引号来包装字符串。仅当表名或列名为MySQL reserved keyword时才需要反引号。

您的代码应如下所示:(tested

class Topic { 
    private $db; 
    public function __construct() { 
        $this->db = new Database; 
    } 
    public function getAllTopics() { 
        $this->db->query("SELECT `name` FROM `users` order by `name` asc"); 
        $results = $this->db->resultset(); 
        return $results;
    }
}

$obj = new Topic();
var_dump($obj->getAllTopics()); // to see the result set