Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.58 KB | None | 0 0
  1. <?php
  2.  
  3. class DB
  4. {
  5.     private static $_instance = null;
  6.     private $_pdo,
  7.         $_query,
  8.         $_error = false,
  9.         $_results,
  10.         $_count = 0;
  11.  
  12.     private function __construct()
  13.     {
  14.         try {
  15.             $this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password'));
  16.         } catch (PDOException $e) {
  17.             die ($e->getMessage());
  18.         }
  19.     }
  20.  
  21.     public static function getInstance()
  22.     {
  23.         if (!isset(self::$_instance)) {
  24.             self::$_instance = new DB();
  25.         }
  26.         return self::$_instance;
  27.     }
  28.  
  29.     public function query($sql, $params = array())
  30.     {
  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.             if ($this->_query->execute()) {
  41.                 $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
  42.                 $this->_count = $this->_query->rowCount();
  43.                 print_r($this->results());
  44.                 die();
  45.             } else {
  46.                 $this->_error = true;
  47.             }
  48.         }
  49.         return $this;
  50.     }
  51.  
  52.     public function action($action, $table, $where = array())
  53.     {
  54.         if (count($where) === 3) {
  55.             $operators = array('=','<','>','<=','>=',"!=");
  56.             $field=        $where[0];
  57.             $operator=     $where[1];
  58.             $value=        $where[2];
  59.             if (in_array($operator,$operators)){
  60.                 $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";
  61.                 if (!$this->query($sql,array($value))->error()){
  62.                     return $this ;
  63.                 }
  64.             }
  65.         }
  66.         return false ;
  67.     }
  68.     public function get($table,$where){
  69.         return $this->action("SELECT *" ,$table,$where);
  70.     }public function delete($table,$where){
  71.         return $this->action("DELETE" ,$table,$where);
  72.     }
  73.     public function insert($table,$fields = array()){
  74.         if (count($fields)){
  75.             $keys=array_keys($fields);
  76.             $values='';
  77.             $x=1;
  78.             $sql= "INSERT INTO {$table} (`".implode("`,`",$keys)."`)";
  79.             foreach ($fields as $field){
  80.                 $values .= "?" ;
  81.                 if ($x<count($fields)){
  82.                     $values .=',';
  83.                 }
  84.                 $x++;
  85.             }
  86.             $sql .= "VALUES (".$values.")";
  87.             if (!$this->query($sql,$fields)->error())
  88.             {die("done !");
  89.                 return true ;
  90.             }
  91.         }
  92.         return false ;
  93.     }
  94.     public function update($table,$id,$fields){
  95.         $set='';
  96.         $x=1;
  97.         foreach ($fields as $name=>$value ){
  98.             $set .= "{$name}=?";
  99.                 if ($x<count($fields)){
  100.                     $set .=',';
  101.                 }
  102.                 $x++;
  103.         }
  104.  
  105.         $sql  = "UPDATE {$table} SET {$set} WHERE id = {$id}";
  106.         if(!$this->query($sql,$fields)->error()){
  107.             return true ;
  108.         }
  109.         return false ;
  110.     }
  111.     public function error()
  112.     {
  113.         return $this->_error;
  114.     }
  115.     public function results(){
  116.         return $this->_results ;
  117.     }
  118.     public function first(){
  119.         return $this->results()[0];
  120.     }
  121.     public function count(){
  122.         return $this->_count ;
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement