此代码出错:致命错误:在第32行的D:\ ProgramFile \ Xampp \ htdocs \ site \ shop \ class \ DB.php中的非对象上调用成员函数prepare()
public function query($sql, $params = array()){
$this->_error= false;
if($this->_query = $this->_pdo->prepare($sql)){
$x = 1;
if(count ($params)){
foreach($params as $param){
$this->_query->bindValue($x, $param);
$x++;
}
}
if($this->_query->execute()){
$this->_result = $this->_query->fetchAll(PDO::FETCH_OBJ);
$this->_count = $this->_query->rowCount();
} else {
$this->_error = true;
}
}
return $this;
}
是什么导致它。对不起,我对PDO和OOP很新。
EDITED 我的PDO declear和__construct函数是
private $_pdo;
private function __construct(){
try {
$this->_pdo = new PDO(
'mysql:host=' . config::get('mysql/host'),//get host name
'dbname=' . config::get('mysql/db'),//get database name
config::get('mysql/username'),
config::get('mysql/password'));
} catch (PDOException $e){
die($e->getMessage());
}
}
答案 0 :(得分:0)
在$this->_pdo = new PDO(...)
之后,您的$this->_pdo
为false
。 var_dump($this->_pdo)
看到它。