Advertisement
fabi0

Untitled

Nov 7th, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.54 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Models;
  4.  
  5. class DB {
  6.  
  7.     private static $_instance = NULL;
  8.     private $_pdo,
  9.             $_query,
  10.             $_error = FALSE,
  11.             $_result,
  12.             $_count = 0;
  13.  
  14.     private function __construct() {
  15.         try {
  16.             $this->_pdo = new \PDO('mysql:host=' . \Models\Config::get('mysql/host') . ';dbname=' . \Models\Config::get('mysql/db'), \Models\Config::get('mysql/username'), \Models\Config::get('mysql/password'));
  17.         } catch (\PDOException $e) {
  18.             die($e->getMessage());
  19.         }
  20.     }
  21.  
  22.     public static function getInstance() {
  23.         if (!isset(self::$_instance)) {
  24.             self::$_instance = new \Models\DB();
  25.         }
  26.         return self::$_instance;
  27.     }
  28.  
  29.     public function query($sql, $params = array()) {
  30.         $this->_error = FALSE;
  31.         if ($this->_query = $this->_pdo->prepare($sql)) {
  32.             $x = 1;
  33.             if (count($params)) {
  34.                 foreach ($params as $params) {
  35.                     $this->_query->bindParam($x, $params);
  36.                     $x++;
  37.                 }
  38.             }
  39.             if ($this->_query->execute()) {
  40.                 $this->_result = $this->_query->fetchAll(\PDO::FETCH_OBJ);
  41.                 $this->_count = $this->_query->rowCount();
  42.             } else {
  43.                 $this->_error = TRUE;
  44.             }
  45.         }
  46.         return $this;
  47.     }
  48.  
  49.     public function error() {
  50.         return $this->_error;
  51.     }
  52.  
  53.     public function getResult() {
  54.         return $this->_result;
  55.     }
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement