尝试按照youtube教程在php上设置我的db类,在该教程中它运行正常,但是在我的机器上“ $ this-> _ query-> execute()”返回false
这是我的课:
class DB {
private static $_instance = null;
private $_pdo, $_query, $_error = false, $_results, $_count = 0, $_lastInsertID = '';
private function __construct() {
try{
$this->_pdo = new PDO('mysql:host=127.0.0.1;dname=database','root','');
} catch (PDOException $e) {
die($e->getMessage());
}
}
public static function get_instance(){
if(!isset(self::$_instance)){
self::$_instance = new DB();
}
return self::$_instance;
}
public function query($sql,$params = []){
$this->_error = false;
if($this->_query = $this->_pdo->prepare($sql)){
$x = 1;
var_dump($params);
if(count($params)){
foreach ($params as $param){
$this->_query->bindValue($x,$param);
$x++;
}
}
if($this->_query->execute()){
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
$this->_count = $this->_query->rowCount();
$this->_lastInsertID = $this->_pdo->lastInsertId();
}else{
$this->_error = true;
}
}
return $this;
}
,这里是调用该函数的php文件:
require_once 'classes/DB.php';
$db = DB::get_instance();
$contact =
['Ewerton','Marschalk','Ewerton@hotmail.com','44444444444','5555555555555'];
$db->query("INSERT INTO contacts (`fname`,`lname`,`email`,`cell_phone`,`home_phone`) values (?,?,?,?,?)",$contact);
为什么这行不通?