Home:ALL Converter>Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: fetch mode requires the classname argument in

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

Ask Time:2018-06-08T21:59:00         Author:tokhy2000

Json Formatter

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

Author:tokhy2000,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/50762319/fatal-error-uncaught-pdoexception-sqlstatehy000-general-error-fetch-mode-r
yy