M
M
Mike Evstropov2016-01-20 10:07:03
PHP
Mike Evstropov, 2016-01-20 10:07:03

How to properly handle PHP PDO responses?

I don’t understand how to tell the PDOStatement object what it would give with SELECT, SHOW, DESCRIBE or EXPLAIN an array, and with the rest TRUE or FALSE? Below is the method that I use for SELECT. How can I fix it so that it processes the responses correctly?

<?php
class Database extends Api
{
  public $settings = [
    'host' 		=> '',
    'name'		=> '',
    'user'		=> '',
    'pass'		=> ''
  ];
  
  private $pdo = null;
  
  public function query ( $string )
  {
    $response = null;
    if( !$this->settings['host'] || !$this->settings['name'] || !$this->settings['user'] || !$this->settings['pass'] || !$string ){
      return $response;
    }elseif( !$this->pdo ){
      $this->pdo = new PDO(
        'mysql:host='. $this->settings['host'] .';dbname='. $this->settings['name'], $this->settings['user'], $this->settings['pass']);
    }
    $response = $this->pdo->query( $string );

    if ( !$response ) return $response;

    $response->rows = [];
    while( $row = $response->fetch( PDO::FETCH_ASSOC ) ){
      $response->rows[] = $row;
    }
    return $response->rows;
  }
}

Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Zelensky, 2016-01-22
@Aroused

Somehow I didn’t check the performance, not where, there may be errors write fix
it If you don’t need a singletone, you can throw it away

<?php 
 class QueryDB extends PDO{
     private static $_instance = null;
     private function __construct ($host, $dbname, $username, $pass) {
         parent::__construct('mysql:host=' . $host . ';dbname=' . $dbname, $username, $pass);
     }
     private function __clone(){}
     public static function getInstance($configDB){
         if (self::$_instance === null) {
             self::$_instance = new self($configDB['host'],$configDB['db'],$configDB['user'], $configDB['pass']);
         }else{
            return self::$_instance; 
         }
     }
     protected function getDbObject(){
         return $this->_instance;
     }
     public function select ($query='', $Parametrs=null) {
         $pdo = $this->getDbObject();
         $sth = $pdo->prepare($query);
         if ($sth->execute($Parametrs)) {
            return $sth->fetchAll(PDO::FETCH_ASSOC);
         }else{
             throw new Exception('Запрос не выполнен'); 
         }
         
     }
     public function insert($table='', $fields=array()) {
         $pdo = $this->getDbObject();
         // для наглядности вообще так делать не стоит
         $values = 'VAlUES(';
         $keys = '(';
         foreach($fields as $key=>$val){
             $values .=$val . ',';
             $keys .=$key . ',';
         }
         trim($values,',');
         $values .= ')';
          trim($keys,',');
         $keys .= ')';
         // конец лапшы
         $sth = $pdo->prepare('INSERT INTO `'.$table.'` '. $keys. ' ' .$values );
         return $sth->execute();
     }
     
 }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question