尝试在php上设置db类,遵循youtube教程,在教程中运行良好,但在我的机器上“$this->\u 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);
为什么这样不行?
暂无答案!
目前还没有任何答案,快来回答吧!