Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.18 KB | None | 0 0
  1. <?php
  2. /*
  3. UserSpice 4
  4. An Open Source PHP User Management System
  5. by the UserSpice Team at http://UserSpice.com
  6.  
  7. This program is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 3 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. class DB2 {
  21.     private static $_instance = null;
  22.     private $_pdo, $_query, $_error = false, $_results, $_resultsArray, $_count = 0, $_lastId, $_queryCount=0;
  23.  
  24.     private function __construct(){
  25.         try{
  26.             $this->_pdo = new PDO('mysql:host=' .
  27.                 Config::get('mysql2/host2') .';dbname='.
  28.                 Config::get('mysql2/db2'),
  29.                 Config::get('mysql2/username2'),
  30.                 Config::get('mysql2/password2'),
  31.                 array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET SESSION sql_mode = ''"));
  32.         } catch(PDOException $e){
  33.             die($e->getMessage());
  34.         }
  35.     }
  36.  
  37.     public static function getInstance(){
  38.         if (!isset(self::$_instance)) {
  39.             self::$_instance = new DB2();
  40.         }
  41.         return self::$_instance;
  42.     }
  43.  
  44.     public function query($sql, $params = array()){
  45.         $this->_queryCount++;
  46.         $this->_error = false;
  47.         if ($this->_query = $this->_pdo->prepare($sql)) {
  48.             $x = 1;
  49.             if (count($params)) {
  50.                 foreach ($params as $param) {
  51.                     $this->_query->bindValue($x, $param);
  52.                     $x++;
  53.                 }
  54.             }
  55.  
  56.             if ($this->_query->execute()) {
  57.                 $this->_results = $this->_query->fetchALL(PDO::FETCH_OBJ);
  58.                 $this->_resultsArray = json_decode(json_encode($this->_results),true);
  59.                 $this->_count = $this->_query->rowCount();
  60.                 $this->_lastId = $this->_pdo->lastInsertId();
  61.             } else{
  62.                 $this->_error = true;
  63.             }
  64.         }
  65.         return $this;
  66.     }
  67.  
  68.     public function findAll($table){
  69.         return $this->action('SELECT *',$table);
  70.     }
  71.  
  72.     public function findById($id,$table){
  73.         return $this->action('SELECT *',$table,array('id','=',$id));
  74.     }
  75.  
  76.     public function action($action, $table, $where = array()){
  77.         $sql = "{$action} FROM {$table}";
  78.         $value = '';
  79.         if (count($where) === 3) {
  80.             $operators = array('=', '>', '<', '>=', '<=');
  81.  
  82.             $field = $where[0];
  83.             $operator = $where[1];
  84.             $value = $where[2];
  85.  
  86.             if(in_array($operator, $operators)){
  87.                 $sql .= " WHERE {$field} {$operator} ?";
  88.             }
  89.         }
  90.         if (!$this->query($sql, array($value))->error()) {
  91.             return $this;
  92.         }
  93.         return false;
  94.     }
  95.  
  96.     public function get($table, $where){
  97.         return $this->action('SELECT *', $table, $where);
  98.     }
  99.  
  100.     public function delete($table, $where){
  101.         return $this->action('DELETE', $table, $where);
  102.     }
  103.  
  104.     public function deleteById($table,$id){
  105.         return $this->action('DELETE',$table,array('id','=',$id));
  106.     }
  107.  
  108.     public function insert($table, $fields = array()){
  109.         $keys = array_keys($fields);
  110.         $values = null;
  111.         $x = 1;
  112.  
  113.         foreach ($fields as $field) {
  114.             $values .= "?";
  115.             if ($x < count($fields)) {
  116.                 $values .= ', ';
  117.             }
  118.             $x++;
  119.         }
  120.  
  121.         $sql = "INSERT INTO {$table} (`". implode('`,`', $keys)."`) VALUES ({$values})";
  122.  
  123.         if (!$this->query($sql, $fields)->error()) {
  124.             return true;
  125.         }
  126.         return false;
  127.     }
  128.  
  129.     public function update($table, $id, $fields){
  130.         $set = '';
  131.         $x = 1;
  132.  
  133.         foreach ($fields as $name => $value) {
  134.             $set .= "{$name} = ?";
  135.             if ($x < count($fields)) {
  136.                 $set .= ', ';
  137.             }
  138.             $x++;
  139.         }
  140.  
  141.         $sql = "UPDATE {$table} SET {$set} WHERE id = {$id}";
  142.  
  143.         if (!$this->query($sql, $fields)->error()) {
  144.             return true;
  145.         }
  146.         return false;
  147.     }
  148.  
  149.     public function results($assoc = false){
  150.         if($assoc) return $this->_resultsArray;
  151.         return $this->_results;
  152.     }
  153.  
  154.     public function first(){
  155.         return $this->results()[0];
  156.     }
  157.  
  158.     public function count(){
  159.         return $this->_count;
  160.     }
  161.  
  162.     public function error(){
  163.         return $this->_error;
  164.     }
  165.  
  166.     public function lastId(){
  167.         return $this->_lastId;
  168.     }
  169.  
  170.     public function getQueryCount(){
  171.         return $this->_queryCount;
  172.     }
  173.  
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement