简体   繁体   中英

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. 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

1- code of Database:

<?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:

<?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

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. Otherwise, how is PDO going to know which class instance it needs to create?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM