Advertisement
benshepherd

database.class.php

Jan 10th, 2014
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.42 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. class DB {
  5.  
  6.         private static $instance;
  7.         private static $con;
  8.         private static $customError;   
  9.         private static $queryObject;   
  10.  
  11.         public static function init($host, $user, $pass, $db) {
  12.  
  13.             if(!isset(self::$instance)) {
  14.  
  15.                 try {
  16.                     self::$instance = new PDO('mysql:host='.$host.';dbname='.$db, $user, $pass);
  17.                     //self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  18.                     self::$con = true;
  19.                 }
  20.                 catch (PDOException $e) {
  21.                    
  22.                     self::$customError = $e->getMessage();
  23.                     self::$con = false;
  24.                 }
  25.             }
  26.         }
  27.        
  28.         public static function isConnected() {
  29.            
  30.             return self::$con; 
  31.         }
  32.        
  33.         public static function select($table, $columns = '*', $where = NULL, $limit = NULL, $column2return = NULL)
  34.         {  
  35.             $params = array();
  36.             $sql = "SELECT ";
  37.            
  38.             if(is_array($columns))
  39.             {
  40.                 for($i = 0; $i < count($columns); $i++)
  41.                 {
  42.                     $c = $columns[$i];
  43.                     $sql .= "`$c`";
  44.                    
  45.                     if($i != count($columns)-1)
  46.                     {
  47.                         $sql .= ", ";  
  48.                     }
  49.                 }
  50.             }
  51.             else
  52.             {
  53.                 if($columns == '*')
  54.                 {
  55.                     $sql .= '*';   
  56.                 }
  57.                 else
  58.                 {
  59.                     $sql .= "`$columns`";  
  60.                 }
  61.             }
  62.            
  63.             $sql .= " FROM `$table`";
  64.            
  65.             if($where != NULL)
  66.             {
  67.                 //Check if there are WHERE parameters
  68.                 if(!is_array($where))
  69.                 {
  70.                     echo 'Error in DB::select function: \'where\' parameter must be Array';
  71.                     return false;
  72.                 }
  73.                
  74.                 //If WHERE is two values, example: user_id, 1
  75.                 //Put them into array for for loop
  76.                 if(!is_array($where[0]) && count($where) == 2)
  77.                 {
  78.                     $where = array($where);
  79.                 }  
  80.                
  81.                 $sql .= " WHERE";
  82.                
  83.                 for($i = 0; $i < count($where); $i++)
  84.                 {
  85.                     //This current website
  86.                     $w = $where[$i];
  87.                    
  88.                     //Only add if array
  89.                     if(is_array($w))
  90.                     {
  91.                         //column = value
  92.                         //Example: user_id = 19
  93.                         if(count($w) == 2)
  94.                         {
  95.                             $sql .= " `$w[0]` = ?";
  96.                             $params[] = $w[1];
  97.                         }
  98.                         //column operator value
  99.                         //array("user_age", ">", 18)
  100.                         //Example: user_age > 18
  101.                         else if(count($w) == 3)
  102.                         {
  103.                             $sql .= " `$w[0]` $w[1] = ?";
  104.                             $params[] = $w[2]; 
  105.                         }
  106.                        
  107.                         //If not last item, append "AND" to query
  108.                         if($i != count($where)-1)
  109.                         {
  110.                             $sql = " AND";     
  111.                         }                      
  112.                     }
  113.                 }
  114.             }
  115.            
  116.             if($limit != NULL && is_int($limit))
  117.             {
  118.                 $sql .= " LIMIT $limit";   
  119.             }
  120.            
  121.             $results = self::query($sql, $params);
  122.            
  123.             if(count($results) == 1)
  124.             {
  125.                 $results = $results[0];
  126.             }
  127.            
  128.             if($column2return != NULL)
  129.             {
  130.                 if(is_array($column2return))
  131.                 {
  132.                     $arr = array();
  133.                    
  134.                     foreach($column2return as $col)
  135.                     {
  136.                         if(isset($results[$col]))
  137.                         {
  138.                             $arr[$col] = $results[$col];   
  139.                         }
  140.                     }
  141.                    
  142.                     return $arr;
  143.                 }
  144.                 else
  145.                 {
  146.                     return $results[$column2return];   
  147.                 }
  148.             }
  149.             else
  150.             {
  151.                 return $results;
  152.             }
  153.         }
  154.  
  155.         public static function query($query, $params = array()) {
  156.            
  157.             try {
  158.                 $params = !is_array($params) ? array($params) : $params;
  159.                 $data = array();
  160.                 $is_select = strtoupper(substr($query,0,6)) == "SELECT" ? true : false;
  161.                
  162.                 if(count($params) > 0) {
  163.                    
  164.                     $stmt = self::$instance->prepare($query);
  165.                    
  166.                     if(!$stmt)
  167.                     {
  168.                         return false;  
  169.                     }
  170.                    
  171.                     $stmt->execute($params);           
  172.                     self::$queryObject = $stmt;
  173.                    
  174.                     if($is_select)
  175.                         $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
  176.                     else {
  177.                         $data = true;  
  178.                     }
  179.                    
  180.                     $stmt->closeCursor();
  181.                 }
  182.                 else {
  183.    
  184.                     $res = self::$instance->query($query);
  185.                     self::$queryObject = $res;
  186.    
  187.                     if(!$res)
  188.                     {                  
  189.                         return false;  
  190.                     }
  191.                     else if($is_select)
  192.                     {
  193.                         foreach($res as $row)
  194.                             $data[] = $row;
  195.                     }
  196.                     else
  197.                     {
  198.                         $data = true;  
  199.                     }
  200.                 }
  201.                
  202.                 self::$customError = "";
  203.                
  204.                 return $data;
  205.             }
  206.             catch(Exception $e) {
  207.                
  208.                 self::setError($e->getMessage());
  209.                 return false;  
  210.             }
  211.         }
  212.        
  213.         public static function getQueryObj()
  214.         {
  215.             return isset(self::$queryObject) ? self::$queryObject : false; 
  216.         }
  217.        
  218.         public static function getError() {
  219.            
  220.             if(strlen(self::$customError) > 0)
  221.             {
  222.                 return self::$customError;
  223.             }
  224.             else
  225.             {
  226.                 $err = self::$instance->errorInfo();   
  227.                 return $err[count($err)-1];
  228.             }
  229.         }
  230.        
  231.         public static function setError($e) {
  232.            
  233.             self::$customError = $e;   
  234.         }
  235.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement