当我使用FETCH_CLASS
时会出现以下错误,但是当我使用FETCH_OBJ
时,页面会正确加载。请帮我解决这个问题。
错误是:
致命错误:未捕获PDOException:SQLSTATE [HY000]:常规错误:获取模式需要C:\ xampp \ htdocs \ Work \ AppDarbNajah \ App \ Database.php中的classname参数:51堆栈跟踪:#0 C:\ xampp \ htdocs \ Work \ AppDarbNajah \ App \ Database.php(51):PDOStatement-> setFetchMode(8)#1 C:\ xampp \ htdocs \ Work \ AppDarbNajah \ App \ Model \ Model.php(62):App \ Database->查询(' SELECT \ r \ n ...',false,' App \ Entity \ Arti ...')#2 C:\ xampp \ htdocs \ Work \ AppDarbNajah \ App \ Model \ ArticleModel.php(9):App \ Model \ Model-> query(' SELECT \ r \ n ...')#3 C:\ xampp \ htdocs \ Work \ AppDarbNajah \ App \ Controller \ ArticleController.php(16):App \ Model \ ArticleModel-> load()#4 C:\ xampp \ htdocs \ Work \ AppDarbNajah \ Public \ index.php(33) :App \ Controller \ ArticleController-> index()#5 {main}在第51行的C:\ xampp \ htdocs \ Work \ AppDarbNajah \ App \ Database.php中抛出
数据库的代码:
<?php
namespace App;
use \PDO;
/* Connect to a MySQL database using driver invocation */
class Database{
private $db_host;
private $db_name;
private $db_user;
private $db_pass;
private $pdo;
public function __construct($db_name,
$db_host = 'localhost',
$db_user = 'root',
$db_pass = ''){
$this->db_name = $db_name;
$this->db_host = $db_host;
$this->db_user = $db_user;
$this->db_pass = $db_pass;
}
private function getPDO() {
if($this->pdo === null){
$pdo = new PDO(
'mysql:host='.$this->db_host.';dbname='.$this->db_name,
$this->db_user,
$this->db_pass
);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->query('
SET NAMES utf8;
SET CHARACTER SET UTF8
');
$this->pdo = $pdo;
}
return $this->pdo;
}
public function query($statement, $one = false, $class = null){
$rs = $this->getPDO()->query($statement);
if(
strpos(strtolower($statement), 'insert') === 0 ||
strpos(strtolower($statement), 'delete') === 0 ||
strpos(strtolower($statement), 'update') === 0
){
return $rs;
}
if($class === null){
$rs->setFetchMode(PDO::FETCH_OBJ);
} else {
$rs->setFetchMode(PDO::FETCH_CLASS);
}
if($one){
$data = $rs->fetch();
} else {
$data = $rs->fetchAll();
}
return $data;
}
public function prepare($statement, $attributes, $one = false, $class = null){
$rs = $this->getPDO()->prepare($statement);
$rst = $rs->execute($attributes);
if(
strpos(strtolower($statement), 'insert') === 0 ||
strpos(strtolower($statement), 'delete') === 0 ||
strpos(strtolower($statement), 'update') === 0
){
return $rst;
}
if($class === null){
$rs->setFetchMode(PDO::FETCH_OBJ);
} else {
$rs->setFetchMode(PDO::FETCH_CLASS);
}
if($one){
$data = $rs->fetch();
} else {
$data = $rs->fetchAll();
}
return $data;
}
}
模型的2代码:
<?php
namespace App\Model;
use App\Database;
class Model{
protected $db;
protected $table;
public function __construct(Database $db){
$this->db = $db;
//var_dump(get_class($this));
}
public function create($fields){
var_dump($fields);
$sql_pairs = [];
$attributes = [];
foreach ($fields as $k =>$v){
$sql_pairs[] = "$k = ?";/*راجع درس PDO*/
$attributes[] = $v;
}
$sql_parts = implode(', ', $sql_pairs);
$this->query("INSERT INTO {$this->table} SET $sql_parts", $attributes);
}
public function update($id, $fields){
}
public function delete($id){
}
public function search($id, $fields = null){
}
public function query($statement, $attributes= null, $one = false){
var_dump(get_class($this));
var_dump(str_replace('Model', 'Entity', get_class($this)));
//die();
if($attributes){
return $this->db->prepare(
$statement,
$attributes,
$one,
str_replace('Model', 'Entity', get_class($this))
);
} else{
return $this->db->query(
$statement,
$one,
str_replace('Model', 'Entity', get_class($this))
);
}
}
}
答案 0 :(得分:0)
这正是错误所说的:
获取模式需要classname参数
你有:
$rs->setFetchMode(PDO::FETCH_CLASS);
如manual所述,可能的情况是:
public bool PDOStatement::setFetchMode ( int $mode )
public bool PDOStatement::setFetchMode ( int $PDO::FETCH_COLUMN , int $colno )
public bool PDOStatement::setFetchMode ( int $PDO::FETCH_CLASS , string $classname , array $ctorargs )
public bool PDOStatement::setFetchMode ( int $PDO::FETCH_INTO , object $object )
如果你想传递PDO::FETCH_CLASS
作为第一个参数,你绝对需要传递另外两个参数。否则,PDO如何知道它需要创建哪个类实例?