title: Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: fetch mode requires the classname argument in
body: <p>When I use <code>FETCH_CLASS</code> the following error come but when I use <code>FETCH_OBJ</code> the page loads correctly. Please help me to solve this.</p>
  
  <p>The error is:</p>
  
  <blockquote>
    <p>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</p>
  </blockquote>
  
  <p>1- code of Database:</p>
  
  <pre><code>&lt;?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-&gt;db_name = $db_name;
          $this-&gt;db_host = $db_host;
          $this-&gt;db_user = $db_user;
          $this-&gt;db_pass = $db_pass;
      }
  
      private function getPDO() {
          if($this-&gt;pdo === null){
              $pdo = new PDO(
                      'mysql:host='.$this-&gt;db_host.';dbname='.$this-&gt;db_name,
                      $this-&gt;db_user,
                      $this-&gt;db_pass
                  );
              $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
              $pdo-&gt;query('
                          SET NAMES utf8;
                          SET CHARACTER SET UTF8
                      ');
              $this-&gt;pdo = $pdo;
          }
          return $this-&gt;pdo;
      }
      public function query($statement, $one = false, $class = null){  
          $rs = $this-&gt;getPDO()-&gt;query($statement);
          if(
             strpos(strtolower($statement), 'insert') === 0 ||
             strpos(strtolower($statement), 'delete') === 0 ||
             strpos(strtolower($statement), 'update') === 0 
  
          ){
              return $rs;
          }
          if($class === null){
              $rs-&gt;setFetchMode(PDO::FETCH_OBJ);
          } else {
              $rs-&gt;setFetchMode(PDO::FETCH_CLASS);
          }
          if($one){
              $data = $rs-&gt;fetch();
          } else {
              $data = $rs-&gt;fetchAll();
          }
  
          return $data;
      }
      public function prepare($statement, $attributes, $one = false, $class = null){
          $rs = $this-&gt;getPDO()-&gt;prepare($statement);
          $rst = $rs-&gt;execute($attributes);
  
          if(
             strpos(strtolower($statement), 'insert') === 0 ||
             strpos(strtolower($statement), 'delete') === 0 ||
             strpos(strtolower($statement), 'update') === 0 
  
          ){
              return $rst;
          }
          if($class === null){
              $rs-&gt;setFetchMode(PDO::FETCH_OBJ);
          } else {
              $rs-&gt;setFetchMode(PDO::FETCH_CLASS);
          }
          if($one){
              $data = $rs-&gt;fetch();
          } else {
              $data = $rs-&gt;fetchAll();
          }
  
          return $data;
      }
  }
  </code></pre>
  
  <p>2- code of model:</p>
  
  <pre><code>&lt;?php
  namespace App\Model;
  use App\Database;
  
  class Model{
    protected $db;
    protected $table;
  
    public function __construct(Database $db){
       $this-&gt;db = $db;
       //var_dump(get_class($this));
    }
    public function create($fields){
       var_dump($fields);
       $sql_pairs = [];
       $attributes = [];
       foreach ($fields as $k =&gt;$v){
          $sql_pairs[] = "$k = ?";/*راجع درس PDO*/
          $attributes[] = $v;
       }
       $sql_parts = implode(', ', $sql_pairs);         
  
       $this-&gt;query("INSERT INTO {$this-&gt;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-&gt;db-&gt;prepare(
             $statement,
             $attributes,
             $one,
             str_replace('Model', 'Entity', get_class($this))
          );
       } else{
          return $this-&gt;db-&gt;query(
             $statement,
             $one,
             str_replace('Model', 'Entity', get_class($this))
          );
       }
    }      
  }   
  </code></pre>