gpt4 book ai didi

php - fatal error : Uncaught PDOException: SQLSTATE[HY000]: General error: fetch mode requires the classname argument in

转载 作者:行者123 更新时间:2023-11-29 10:12:20 28 4
gpt4 key购买 nike

当我使用FETCH_CLASS时,出现以下错误,但当我使用FETCH_OBJ时,页面加载正确。请帮我解决这个问题。

错误是:

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: fetch mode requires the classname argument in C:\xampp\htdocs\Work\AppDarbNajah\App\Database.php:51 Stack trace: #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->query('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} thrown in C:\xampp\htdocs\Work\AppDarbNajah\App\Database.php on line 51

1-数据库代码:

<?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))
);
}
}
}

最佳答案

这正是错误所说的:

fetch mode requires the classname argument

你有:

$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 如何知道它需要创建哪个类实例?

关于php - fatal error : Uncaught PDOException: SQLSTATE[HY000]: General error: fetch mode requires the classname argument in,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50762319/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com