Advertisement
theminer3746

DB.php

Nov 19th, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.70 KB | None | 0 0
  1. <?php
  2. class DB{
  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(
  13.             'mysql:host=' . config::get('mysql/host'),//get host name
  14.             'dbname=' . config::get('mysql/db'),//get database name
  15.             config::get('mysql/username'),
  16.             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 DB();
  25.             //echo "Connected";
  26.         }
  27.         return self::$_instance;
  28.     }
  29.    
  30.     public function query($sql, $params = array()){
  31.         $this->_error= false;
  32.         if($this->_query = $this->_pdo->prepare($sql)){
  33.             $x = 1;
  34.             if(count ($params)){
  35.                 foreach($params as $param){
  36.                     $this->_query->bindValue($x, $param);
  37.                     $x++;
  38.                 }
  39.             }
  40.            
  41.             if($this->_query->execute()){
  42.                 $this->_result = $this->_query->fetchAll(PDO::FETCH_OBJ);
  43.                 $this->_count = $this->_query->rowCount();
  44.             } else {
  45.                 $this->_error = true;
  46.             }
  47.         }
  48.        
  49.         return $this;
  50.     }
  51.    
  52.     public function action($action, $table, $where = array()){
  53.         if(count($where) === 3){
  54.             $operators = array('=','>','<','>=','<=');
  55.            
  56.             $field      = $where[0];
  57.             $operator   = $where[1];
  58.             $value      = $where[2];
  59.            
  60.             if(in_array($operator, $operators)){
  61.                 $sql = "{$action} * FROM {$table} WHERE {$field} {$operator} ?";   
  62.                
  63.                 if(!$this->query($sql, array($value))->error()){
  64.                     return $this;
  65.                 }
  66.             }
  67.         }
  68.         return false;
  69.     }
  70.    
  71.     public function get($table, $where){
  72.         return $this->action('SELECT *', $table, $where);
  73.     }
  74.    
  75.     public function delete($table, $where){
  76.         return $this->action('DELETE', $table, $where);
  77.     }
  78.     public function insert($table, $fields = array()){
  79.         if(count($fields)){
  80.             $keys = array_key($fields);
  81.  
  82.              $sql = "INSERT INTO users (".implode(', ', $keys).") VALUES (".implode(',', array_fill(0, count($keys), '?')).")";
  83.              
  84.              if(!$this->query($sql, $fields)->error()){
  85.                 return true;
  86.              }
  87.         }
  88.         return false;
  89.     }
  90.    
  91.     public function update($table, $id, $fields){
  92.         $set = '';
  93.         $x = 1;
  94.        
  95.         foreach($fields as $name => $value){
  96.             $set .= "{$name} = ?";
  97.             if($x < count($fields)){
  98.                 $set .= ', ';
  99.             }
  100.             $x++;
  101.         }
  102.         die($set);
  103.        
  104.         $sql = "UPDATE {$table} SET {$set} WHERE id = {$id}";
  105.        
  106.         if(!$this->query($sql, $fields)->error()){
  107.             return true;
  108.         }
  109.         return false;
  110.     }
  111.    
  112.     public function results(){
  113.         return $this->_results;
  114.     }
  115.    
  116.     public function first(){
  117.         return $this->_results[0]; 
  118.     }
  119.    
  120.     public function error(){
  121.         return $this->_error;  
  122.     }
  123.    
  124.     public function count(){
  125.         return $this->$count;
  126.     }
  127. }
  128. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement