Advertisement
Guest User

Blabla

a guest
Sep 3rd, 2014
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.78 KB | None | 0 0
  1. <?php
  2.     class MYSQL {
  3.         private static $_instance = null;
  4.         private $_pdo,
  5.                 $_query,
  6.                 $_error = false,
  7.                 $_result,
  8.                 $_count = 0;
  9.  
  10.         private function __construct() {
  11.             try {
  12.                 $this->_pdo = new PDO('mysql:host=' . CONFIG::getConfig('mysql/host') . ';dbname=' . CONFIG::getConfig('mysql/db'), CONFIG::getConfig('mysql/user'), CONFIG::getConfig('mysql/pass'));
  13.             } catch(PDOException $e) {
  14.                 die($e->getMessage());
  15.             }
  16.         }
  17.  
  18.         public static function getInstance() {
  19.             if(!isset(self::$_instance)) {
  20.                 self::$_instance = new MYSQL();
  21.             }
  22.             return self::$_instance;
  23.         }
  24.  
  25.         public function query($sql, $params = array()) {
  26.             $this->_error = false;
  27.             $x = 1;
  28.             if($this->_query = $this->_pdo->prepare($sql)) {
  29.                 if(count($params)) {
  30.                     foreach($params as $param) {
  31.                         $this->_query->bindValue($x, $param);
  32.                         $x++;
  33.                     }
  34.                 }
  35.  
  36.                 if($this->_query->execute()) {
  37.                     $this->_result = $this->_query->fetchAll(PDO::FETCH_OBJ);
  38.                     $this->_count = $this->_query->rowCount();
  39.                 } else {
  40.                     $this->_error = true;
  41.                 }
  42.             }
  43.  
  44.             return $this;
  45.         }
  46.  
  47.         public static function insert($table, $fields = array()) {
  48.             if(count($fields)) {
  49.                 $keys = array_keys($fields);
  50.                 $values = null;
  51.                 $x = 1;
  52.  
  53.                 foreach ($fields as $field) {
  54.                     $values .= "?";
  55.                     if($x < count($fields)) {
  56.                         $values .= ', ';
  57.                     }
  58.                     $x++;
  59.                 }
  60.  
  61.                 $sql = "INSERT INTO " . $table . " (`" . implode('`, `', $keys) . "`) VALUES ({$values})";
  62.                
  63.                 if(!$this->query($sql, $fields)->error()) {
  64.                     return true;
  65.                 }
  66.             }
  67.             return false;
  68.         }
  69.  
  70.         public function result() {
  71.             return $this->_result;
  72.         }
  73.  
  74.         public function error() {
  75.             return $this->_error;
  76.         }
  77.  
  78.         public function count() {
  79.             return $this->_count;
  80.         }
  81.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement