Advertisement
benshepherd

Sql Helper (untested)

Aug 13th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.92 KB | None | 0 0
  1. <?php
  2.  
  3.     class DB {
  4.        
  5.         public static $db;
  6.         public static $results = array();
  7.         public static $con = false;
  8.        
  9.         //Initializes database connection
  10.         public static function init($server, $uid, $pwd, $db) {
  11.            
  12.             if(!isset(self::$db)) {
  13.                
  14.                 //Connect to database
  15.                 self::$db = @new mysqli($server, $uid, $pwd, $db);
  16.                
  17.                 //Check connection
  18.                 if(self::$db->connect_errno > 0) {
  19.                    
  20.                     self::UpdateResult(self::$db->connect_error, false);
  21.                     return false;
  22.                 }
  23.                 else {
  24.                    
  25.                     self::$con = true;
  26.                        
  27.                 }
  28.             }  
  29.            
  30.             return true;           
  31.         }
  32.        
  33.         //Returns escaped values
  34.         public static function escape($value) {
  35.             return self::$db->real_escape_string($value);
  36.         }
  37.        
  38.         //Returns true or false
  39.         public static function simple_query($q) {
  40.             $q = @self::$db->query($q) ? true : false;
  41.            
  42.             if($q) {
  43.                 return true;
  44.             }
  45.             else {
  46.                 self::UpdateResult(self::$db->error, false);
  47.                 return false;  
  48.             }
  49.         }
  50.        
  51.         //Returns the query object
  52.         public static function query($q) {
  53.             $q = @self::$db->query($q);
  54.            
  55.             if(!$q) {
  56.                 self::UpdateResult(self::$db->error, false);
  57.             }
  58.            
  59.             return $q;
  60.         }
  61.        
  62.         //Select statement
  63.         public static function select($table, $rows = '*', $where = null, $order = null) {
  64.            
  65.             $q = "SELECT $rows FROM $table";
  66.            
  67.             if($where != null) {
  68.                 $q .= " WHERE ".$where;
  69.             }
  70.             if($order != null) {
  71.                 $q .= " ORDER BY ".$order; 
  72.             }
  73.            
  74.             echo $q."<br>";
  75.            
  76.             //Check if table exists
  77.             if(self::tableExists($table)) {
  78.                
  79.                 $query = self::query($q);
  80.                
  81.                 if($query) {
  82.                    
  83.                     //Clear results
  84.                     self::$results = array();
  85.                    
  86.                     //Add results                  
  87.                     while($r = $query->fetch_assoc()) {            
  88.                         self::UpdateResult($r);
  89.                     }
  90.                    
  91.                     return true;
  92.                 }
  93.                 else {
  94.                     self::UpdateResult(self::$db->error, false);   
  95.                 }
  96.             }
  97.            
  98.             return false;
  99.         }
  100.        
  101.         public static function insert($table, $rows = null, $params) {
  102.            
  103.             $q = "INSERT INTO $table";
  104.            
  105.             if($rows != null) {
  106.                
  107.                 if(!is_array($rows)) {
  108.                     $rows = preg_split("/,/", $rows);  
  109.                 }
  110.                
  111.                 $q .= " (".implode(", ", $rows).")";
  112.             }
  113.            
  114.             $q .= " VALUES(";
  115.            
  116.             if(!is_array($params))
  117.                 $params = preg_split("/,/", $params);
  118.            
  119.             for($i = 0; $i < count($params); $i++) {
  120.                
  121.                 $param = $params[$i];
  122.                
  123.                 if(is_int($param)) {
  124.                     $q .= $param;  
  125.                 }
  126.                 else {
  127.                     $q .= "'".$param."'";  
  128.                 }
  129.                
  130.                 if($i != count($params) - 1)
  131.                     $q .= ", ";
  132.                    
  133.             }
  134.            
  135.             $q .= ");";
  136.            
  137.             return self::simple_query($q);
  138.         }
  139.        
  140.         public static function update($table, $set, $where = null) {
  141.            
  142.             $q = "UPDATE $table SET";
  143.  
  144.             //If set is an array
  145.             if(is_array($set)) {
  146.                
  147.                 //If set is multiple arrays
  148.                 if(is_array($set[0])) {
  149.                    
  150.                     for($i = 0; $i < count($set); $i++) {
  151.                        
  152.                         $row = $set[$i][0];
  153.                         $value = $set[$i][1];
  154.                        
  155.                         $q .= " ".$row." = ";
  156.                        
  157.                         if(is_int($value)) {
  158.                             $q .= $value;  
  159.                         }
  160.                         else {
  161.                             $q .= "'$value'";  
  162.                         }
  163.                        
  164.                         if($i != count($set) - 1)
  165.                             $q .= ", ";
  166.                        
  167.                     }  
  168.                 }
  169.                 //Single array
  170.                 else {
  171.                
  172.                     $q .= " ".$set[0]." = ";
  173.                    
  174.                     if(is_int($set[1])) {
  175.                         $q .= $set[1]; 
  176.                     }
  177.                     else {
  178.                         $q .= "'".$set[1]."'"; 
  179.                     }  
  180.                 }
  181.             }
  182.             else {
  183.                
  184.                 if(!strstr($set, ",")) {
  185.                    
  186.                     $data = explode("=", $set);
  187.                    
  188.                     $q .= " ".$data[0]." = ";
  189.                    
  190.                     if(is_int($data[1])) {
  191.                        
  192.                         $q .= $data[1];
  193.                     }
  194.                     else {
  195.                        
  196.                         $q .= "'".$data[1]."'";
  197.                     }
  198.                 }
  199.                 else {
  200.                    
  201.                     $datas = explode(",", $set);
  202.                    
  203.                     for($i = 0; $i < count($datas); $i++) {
  204.                        
  205.                         $data = explode("=", $datas[$i]);
  206.                        
  207.                         $q .= " ".$data[0]." = ";
  208.                        
  209.                         if(is_int($data[1])) {
  210.                            
  211.                             $q .= $data[1];
  212.                         }
  213.                         else {
  214.                            
  215.                             $q .= "'".$data[1]."'";
  216.                         }
  217.                        
  218.                         if($i != count($datas) - 1)
  219.                             $q .= ", ";                
  220.                     }
  221.                        
  222.                 }  
  223.             }
  224.            
  225.             if($where != null) {
  226.                
  227.                 $q .= " WHERE ";
  228.                
  229.                 if(!is_array($where) && strstr($where, ","))
  230.                     $where = preg_split("/,/", $where);
  231.                
  232.                 if(is_array($where)) {
  233.                    
  234.                     $q .= implode(" AND ", $where);
  235.                 }
  236.                 else {
  237.                    
  238.                     $q .= $where;
  239.                 }
  240.             }
  241.                
  242.             echo $q."<br>";
  243.            
  244.             return self::simple_query($q);
  245.         }
  246.        
  247.         public static function delete($table, $where = null) {
  248.            
  249.             if($where != null) {
  250.                
  251.                 $q = "DELETE FROM $table WHERE ".$where;
  252.                
  253.             }
  254.             else {
  255.                
  256.                 $q = "DROP TABLE $table";
  257.                    
  258.             }
  259.            
  260.             echo $q."<br>";
  261.  
  262.             return self::simple_query($q);
  263.                
  264.         }
  265.        
  266.         public static function createTable($name, $data) {
  267.            
  268.             if(self::tableExists($name))
  269.                 return true;
  270.            
  271.             $q = "CREATE TABLE $name (";
  272.            
  273.             if(!is_array($data)) {
  274.                 $data = preg_split("/,/", $data);
  275.             }
  276.            
  277.             for($i = 0; $i < count($data); $i++) {
  278.                
  279.                 $q .= implode(" ", explode("=", $data[$i]));   
  280.                
  281.                 if($i != count($data) - 1)
  282.                     $q .= ", ";
  283.             }
  284.            
  285.             $q .= ");";
  286.            
  287.             return self::simple_query($q); 
  288.         }
  289.        
  290.         public static function emptyTable($name) {
  291.             return self::simple_query("TRUNCATE $name");
  292.         }
  293.        
  294.         public static function tableExists($table) {
  295.            
  296.             $q = @self::query("SHOW TABLES LIKE '$table'");
  297.            
  298.             if($q) {
  299.                 if($q->num_rows > 0)
  300.                     return true;
  301.                 else
  302.                     return false;
  303.                
  304.             } else {
  305.                 return false;  
  306.             }
  307.         }
  308.        
  309.         private static function UpdateResult($e, $append = true) {
  310.             if($append)
  311.                 array_push(self::$results, $e);
  312.             else
  313.                 self::$results = $e;
  314.         }
  315.        
  316.        
  317.         public static function getResults() {
  318.            
  319.             //Save result
  320.             $v = self::$results;
  321.            
  322.             echo "Count: ".count($v)."<br>";
  323.            
  324.             //Clear result
  325.             self::$results = array();
  326.            
  327.             //Return saved result
  328.             return $v;
  329.             //return count($v) == 1 ? $v[0] : $v;
  330.         }
  331.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement