简体   繁体   English

致命错误:未被捕获的PDOException:SQLSTATE [HY000]:常规错误:提取模式要求在

[英]Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: fetch mode requires the classname argument in

When I use FETCH_CLASS the following error come but when I use FETCH_OBJ the page loads correctly. 当我使用FETCH_CLASS ,会出现以下错误,但是当我使用FETCH_OBJ ,页面将正确加载。 Please help me to solve this. 请帮我解决这个问题。

The error is: 错误是:

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 致命错误:未捕获的PDOException:SQLSTATE [HY000]:常规错误:访存模式需要C:\\ xampp \\ htdocs \\ Work \\ AppDarbNajah \\ App \\ Database.php: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-> 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}放在C:\\ xampp \\ htdocs \\第51行的Work \\ AppDarbNajah \\ App \\ Database.php

1- code of Database: 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- code of model: 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))
        );
     }
  }      
}   

It's exactly what the error says: 错误确切地说:

fetch mode requires the classname argument 提取模式需要classname参数

You have: 你有:

$rs->setFetchMode(PDO::FETCH_CLASS);

And the possible cases, as stated in the manual , are: 手册中所述,可能的情况是:

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 )

If you want to pass PDO::FETCH_CLASS as first argument you absolutely need to pass the two other arguments. 如果要传递PDO::FETCH_CLASS作为第一个参数,则绝对需要传递其他两个参数。 Otherwise, how is PDO going to know which class instance it needs to create? 否则,PDO如何知道需要创建哪个类实例?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 致命错误:未捕获的 PDOException:SQLSTATE[HY000]:一般错误 - Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error 致命错误:带有消息“ SQLSTATE [HY000]:常规错误”的未捕获异常“ PDOException” .. C:\\ xampp \\ .. PDOStatement-&gt; fetch()..第83行 - Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error' .. C:\xampp\..PDOStatement->fetch().. on line 83 致命错误:未捕获异常&#39;PDOException&#39;,消息&#39;SQLSTATE [HY000]:常规错误&#39;中 - Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error' in PHP:致命错误:未捕获的 PDOException:SQLSTATE[HY000] - PHP: Fatal error: Uncaught PDOException: SQLSTATE[HY000] 致命错误:未捕获的 PDOException:SQLSTATE [HY000]:一般错误:2014 存在未决结果集时无法执行查询 - Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 2014 Cannot execute queries while there are pending result sets 邮递员:未捕获的PDOException:SQLSTATE [HY000]:常规错误:1366不正确的整数值:&#39;&#39; - Postman: Uncaught PDOException: SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' Azure 应用服务:未捕获的 PDOException:SQLSTATE[HY000]:一般错误:5 数据库已锁定 - Azure App Service: Uncaught PDOException: SQLSTATE[HY000]: General error: 5 database is locked Php 致命错误:未捕获的 PDOException:SQLSTATE[HY000] [1045] 用户 &#39;fobos&#39;@&#39;localhost&#39; 访问被拒绝(使用密码:YES) - Php Fatal error: Uncaught PDOException: SQLSTATE[HY000] [1045] Access denied for user 'fobos'@'localhost' (using password: YES) 为什么SQLSTATE [HY000]:一般错误? - Why SQLSTATE[HY000]: General error? SQLSTATE [HY000]:一般错误:2053 - SQLSTATE[HY000]: General error: 2053
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM