在非对象 - PDO DB类上调用成员函数prepare()

时间:2014-09-18 23:10:56

标签: php class oop pdo

尝试遵循OOP PDO教程,该教程是创建数据库类的指南。

但是在尝试使用此类时,我收到以下错误。

Fatal error: Call to a member function prepare() on a non-object

它指向的是这个函数:

public function query($query){
  $this->stmt = $this->dbh->prepare($query);
}

之前的课程是:

class Database {
private $host      = 'localhost:8889';
private $user      = 'root';
private $pass      = 'root';
private $dbname    = 'PHPGamble';

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 instanace
    try{ 
        $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
    }
    // Catch any errors
    catch(PDOException $e){
        $this->error = $e->getMessage();
    }
}

    public function query($query){
    $this->stmt = $this->dbh->prepare($query);
    }

我正在尝试使用这样的类:

  require('../libs/PHP_Classes/class.database.php');
  $database = new Database();

  // insert new table
  $database->query('INSERT INTO tables (table_buyin) VALUES (:table_buyin)');
  $database->bind(':table_buyin', '50');
  $database->execute();

任何有关出错的帮助?

感谢。

0 个答案:

没有答案