有人可以指出我为什么以下代码失败的正确方向..
MySQLDb.php:
class MySQLDb
{
protected $_mysql;
protected $_query;
function __constructor($host,$username,$password,$db)
{
$this->_mysql = new mysqli($host,$username,$password,$db) or die('Problem connecting to the DB.');
}
function query($query)
{
$this->_query = filter_var($query,FILTER_SANITIZE_STRING);
$stmt = $this->_prepareQuery();
$stmt->execute();
$results = $stmt->_dynamicBindResult($stmt);
}
protected function _prepareQuery(){
$dbh = $this->_mysql;
if ( !$stmt = $dbh->prepare($this->_query) ) {
trigger_error('Problem preparing query', E_USER_ERROR);
}
return $stmt;
}
索引.php:
require_once('MYSQLDb.php');
$Db = new MySQLDb('localhost','root','','blog');
$results =$Db->query("SELECT * FROM posts");
当我运行程序时,它会fatal error: call to a member function prepare on a non-object
在if ( !$stmt = $dbh->prepare($this->_query) ) {
我正在尝试学习做这些事情的面向对象的方式,所以不太了解如何解决这个问题。
谢谢。