Advertisement
kotvalera83

PHP/MySQL

Feb 8th, 2014
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.48 KB | None | 0 0
  1. <?php
  2. //error_reporting(E_ALL);
  3.  
  4. class MySqlTable {
  5.    
  6.     var $tableName;
  7.     var $link;
  8.     var $db_name;
  9.    
  10.     function MySqlTable($tableName='') {
  11.         $this->tableName = $tableName;
  12.         $c1 = Db_connection::getInstance();
  13.         $db_connection = $c1->getDbConnection();
  14.         $this->link = $db_connection['link'];
  15.         $this->db_name = $db_connection['db_name'];
  16.     }
  17.    
  18.     function escape($value) {
  19.         $value = mysqli_real_escape_string($this->link,$value);
  20.         return $value;
  21.     }
  22.    
  23.     function loadByFields($fields, $values, $options=array()) {
  24.         $fields = explode(',',$fields);
  25.         $values = explode(',',$values);
  26.         $selected_fields = $options['fields'];
  27.         $display = $options['display'];
  28.        
  29.         $sqlSelect = $this->getSQLSelectByFields($selected_fields);
  30.        
  31.         $query = "SELECT ".$sqlSelect." FROM ". $this->tableName ." WHERE";
  32.         for($i=0; $i<count($fields); $i++) {
  33.             if($i==0) $query .= " " .$this->escape($fields[$i]). " = '" .$this->escape($values[$i]). "'";
  34.             else $query .= " AND " .$this->escape($fields[$i]). " = '" .$this->escape($values[$i]). "'";
  35.         }
  36.        
  37.         if($options!="") {
  38.             foreach($options as $i => $v) {
  39.                 if($i=='group') $groupByCond = " GROUP BY ".$this->escape($v)." ";
  40.                 if($i=='order') $orderByCond = " ORDER BY ".$this->escape($v)." ";
  41.                 if($i=='limit') $limitCond = " limit ".$this->escape($v)." ";
  42.             }
  43.             $query .= $groupByCond.$orderByCond.$limitCond;
  44.         }
  45.        
  46.         if($display=='1') echo $query;
  47.        
  48.         return $this->loadArrayFromQuery($query);
  49.     }
  50.    
  51.     function selectAll($options=array()) {
  52.         $fields = $options['fields'];
  53.         $display = $options['display'];
  54.        
  55.         $sqlSelect = $this->getSQLSelectByFields($fields);
  56.        
  57.         $array = $this->loadIntoArray();
  58.         $query = "SELECT ".$sqlSelect." FROM ". $this->tableName." WHERE 1 ";
  59.         foreach($array as $key=>$value) {
  60.             if($value!="")
  61.                 $query .= " AND ".$key." = '".$this->escape($value)."'";
  62.         }
  63.        
  64.         if($options!="") {
  65.             foreach($options as $i => $v) {
  66.                 if($i=='group') $groupByCond = " GROUP BY ".$this->escape($v)." ";
  67.                 if($i=='order') $orderByCond = " ORDER BY ".$this->escape($v)." ";
  68.                 if($i=='limit') $limitCond = " limit ".$this->escape($v)." ";
  69.             }
  70.             $query .= $groupByCond.$orderByCond.$limitCond;
  71.         }
  72.        
  73.         if($display=='1') echo $query;
  74.        
  75.         return $this->loadArrayFromQuery($query);
  76.     }
  77.    
  78.     function customQuery($query) {
  79.         $return = $this->loadArrayFromQuery($query);
  80.         return $return;
  81.     }
  82.    
  83.     function getSQLSelectByFields($fields) {
  84.         $fieldsArray = explode(',',$fields);
  85.        
  86.         if($fields=='') {
  87.             $sqlSelect = "*";
  88.         }
  89.         else {
  90.             $sqlSelect = '';
  91.             $i=0;
  92.             foreach($fieldsArray as $value) {
  93.                 if($i==0) $sqlSelect = $value;
  94.                 else $sqlSelect = $sqlSelect.', '.$value;
  95.                 $i++;
  96.             }
  97.         }
  98.         return $sqlSelect;
  99.     }
  100.    
  101.     function insert($options=array()) {
  102.         $display = $options['display'];
  103.        
  104.         $array = $this->loadIntoArray();
  105.        
  106.         $query = "INSERT INTO ". $this->tableName." (";
  107.         $s = "";
  108.         foreach($array as $key => $value) {
  109.             if($value || is_numeric($value)) {
  110.                 $query.= $s.$key;
  111.                 $s = ", ";
  112.             }
  113.         }
  114.         $query .= ") VALUES (";
  115.         $s = "";
  116.         foreach($array as $value) {
  117.             if($value || is_numeric($value)) {
  118.                 $query.= $s."'".$this->escape($value)."'";
  119.                 $s = ", ";
  120.             }
  121.         }
  122.         $query .= ")";
  123.        
  124.         if($display=='1') echo $query;
  125.        
  126.         $this->executeQuery($query);
  127.         $result = mysqli_insert_id($this->link);
  128.        
  129.         return $result;
  130.     }
  131.    
  132.     function delete($id) {
  133.         $query = "DELETE FROM ". $this->tableName." WHERE id = '".$this->escape($id)."'";
  134.         $this->executeQuery($query);
  135.     }
  136.    
  137.     function updateByFields($fields,$id,$options=array()) {
  138.         $display = $options['display'];
  139.        
  140.         // Get the id of the table...
  141.         $array = $this->loadIntoArray();
  142.         $i=0;
  143.         foreach($array as $key=>$value) {
  144.             if($i==0) {
  145.                 $table_id=$key;
  146.                 break;
  147.             }
  148.             $i++;
  149.         }
  150.         // END Get the id of the table...
  151.        
  152.         $query = "UPDATE ". $this->tableName." SET ";
  153.         $i=0;
  154.         foreach($fields as $key=>$value) {
  155.             if($i==0) $query.= $key." = '".$this->escape($value)."'";
  156.             else $query.= ", ".$key." = '".$this->escape($value)."'";
  157.             $i++;
  158.         }
  159.        
  160.         if(is_array($id)) {
  161.             $query .= " WHERE 1 ";
  162.             foreach($id as $key=>$value) {
  163.                 $query .= " AND ".$key." = '".$this->escape($value)."'";;
  164.             }
  165.         }
  166.         else {
  167.             $query .=" WHERE ".$table_id." = '".$this->escape($id)."'";
  168.         }
  169.        
  170.         if($display=='1') echo $query;
  171.        
  172.         return $this->executeQuery($query);
  173.     }
  174.    
  175.     function update($id) {
  176.         $array = $this->loadIntoArray();
  177.         $query = "UPDATE ". $this->tableName." SET ";
  178.         $s = "";
  179.         foreach($array as $key=>$value) {
  180.             if($value) {
  181.                 $query.= $s.$key." = '".$this->escape($value)."'";
  182.                 $s = ",";
  183.             }
  184.             elseif(is_numeric($value)) {
  185.                 $query.= $s.$key." = '".$this->escape($value)."'";
  186.                 $s = ",";
  187.             }
  188.         }
  189.         $query .=" WHERE id = '".$id."'";
  190.         //echo $query;
  191.        
  192.         $this->executeQuery($query);
  193.     }
  194.    
  195.     function counter() {
  196.         $sql = 'UPDATE count set count=count+1 WHERE id=1';
  197.         mysqli_query($this->link, $sql);
  198.     }
  199.    
  200.     function executeQuery($query) {
  201.         //echo $query.'<br>';
  202.         //echo 'db: '.$this->db_name.'<br>';
  203.         mysqli_select_db($this->link, $this->db_name);
  204.         //$this->counter();
  205.         $result = mysqli_query($this->link, $query);
  206.         return $result;
  207.     }
  208.    
  209.     //execute the query and return an array of corresponding MySqlTable inherited object
  210.     function loadArrayFromQuery($query) {
  211.         $result = $this->executeQuery($query);
  212.         $return = array();
  213.         while ($rows = mysqli_fetch_assoc($result)) {
  214.             $return[] = $rows;
  215.         }
  216.         return $return;
  217.     }
  218. }
  219.  
  220. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement