Advertisement
eyuprog

Mysqli library OOP

Mar 1st, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.11 KB | None | 0 0
  1. <?php
  2.  
  3. class IKS_DB_MySqli extends mysqli{
  4.     var $default_fetch_mode = 'object';
  5.     var $last_query = null;
  6.     var $last_result = null;
  7.    
  8.     protected $last_insert_id = null;
  9.    
  10.     protected $last_affected_rows = null;
  11.    
  12.     protected $statementmgr = null;
  13.    
  14.     function __construct($db_name=DB_NAME, $db_hostname=DB_HOSTNAME, $db_username=DB_USERNAME, $db_password=DB_PASSWORD){
  15.       if(!isset($db_hostname) || !isset($db_username) || !isset($db_password) || !isset($db_name)) return;
  16.       else{
  17.           parent::__construct($db_hostname, $db_username, $db_password, $db_name);
  18.         }
  19.     }
  20.  
  21.     function SetFetchMode($mode='assoc'){
  22.       $mode = strtolower($mode);
  23.       if($mode != 'assoc' && $mode != 'object') $mode = 'assoc';
  24.       $this->default_fetch_mode = $mode;
  25.     }
  26.    
  27.     function affected_rows(){
  28.       return $this->last_affected_rows;    
  29.     }
  30.  
  31.     function insert_id(){
  32.       return $this->last_insert_id;
  33.     }
  34.  
  35.    
  36.     function query($query, $replacements=null, $result_mode=MYSQLI_STORE_RESULT){
  37.       if((is_array($replacements) or is_object($replacements)) and !empty($replacements)){
  38.         return $this->prepared_query($query, $replacements);
  39.       }else{
  40.           $this->last_result = parent::query($query, $result_mode) or
  41.           die("query error: ".$this->error."<br />Query: $query");
  42.           $this->last_query = $query;
  43.           $this->last_affected_rows = $this->affected_rows;
  44.           $this->last_insert_id = $this->insert_id;
  45.         }
  46.         return $this->last_result;
  47.     }
  48.    
  49.  
  50.     function prepared_query($query, $replacements=null){
  51.       if((!is_array($replacements) and !is_object($replacements)) or empty($replacements)) $replacements = null;
  52.      
  53.       $replacements = $this->preparse_prepared_query($query, $replacements);
  54.      
  55.       if($query != $this->last_query){
  56.      
  57.         $this->free_result();
  58.        
  59.         unset($this->last_result, $this->statementmgr);
  60.        
  61.         $this->last_result =$this->prepare($query) or die($this->errno);
  62.        
  63.         $this->statementmgr = new MySQLiManager($this->last_result, $replacements);
  64.       }else{
  65.             $this->statementmgr->run_stmt($replacements);
  66.       }
  67.       $this->last_query = $query;
  68.       $this->last_affected_rows = $this->statementmgr->affected_rows();
  69.       $this->last_insert_id = $this->statementmgr->insert_id();
  70.       return $this->statementmgr;
  71.     }
  72.    
  73.     function preparse_prepared_query(&$query, $replacements){
  74.       if(!$replacements) return;
  75.       if(is_object($replacements)) $replacements = (array) $replacements;
  76.       $params = array();
  77.    
  78.       $last_param_at = 0;
  79.    
  80.       foreach($replacements as $k=>$v){
  81.         if(strpos($k,":") === false){
  82.           continue;
  83.         }else{
  84.        
  85.           while(true){
  86.          
  87.             $k_idx = strpos($query, $k);
  88.             if($k_idx === false) break;
  89.             if(is_null($v) or $v == '') $v = 'NULL';
  90.             $params[$k_idx] = $v;
  91.             $query = preg_replace('/'.preg_quote($k,'/').'/', '@', $query, 1);
  92.           }
  93.         }
  94.       }
  95.      
  96.       foreach($replacements as $k=>$v){
  97.         if(strpos($k,":") !== false){
  98.           continue;
  99.         }else{
  100.          
  101.           $q_idx = strpos($query, '?');
  102.           if(is_null($v) or $v == '') $v = 'NULL';
  103.           $params[$q_idx] = $v;
  104.           $query = preg_replace('/'.preg_quote('?','/').'/', '@', $query, 1);
  105.         }
  106.       }
  107.    
  108.       $query = str_replace('@','?',$query);
  109.    
  110.     ksort($params);
  111.     return $params;
  112.     }
  113.    
  114.    
  115.     function free_result(){
  116.       if(isset($this->statementmgr)) $this->statementmgr->free_result();
  117.     }
  118.  
  119.     function ListTables(){
  120.         $result = $this->query('SHOW TABLES');
  121.         while( $row = $result->fetch_row())
  122.             $tables[] = $row[0];
  123.         return $tables;
  124.     }
  125.  
  126.  
  127.     function ListFields($table){
  128.         $q="SELECT * FROM `$table` LIMIT 1";
  129.         $r = $this->FieldRow($q);
  130.         return array_keys((array) $r);
  131.     }
  132.  
  133.    
  134.     function insert($table, $data=null){
  135.         $fields='';
  136.         $values='';
  137.       if(!$data) return false;
  138.       if(is_object($data)) $data = (array) $data;
  139.       $first=true;
  140.         foreach($data as $fieldname=>$value){
  141.             if(!$first){
  142.                 $fields.=', ';
  143.                 $values.=', ';
  144.           }
  145.             $first=false;
  146.             $hook = ":\\$fieldname/:";
  147.             $fields.="`$fieldname`";
  148.             $values.=$hook;
  149.             $params[$hook] = $value;
  150.         }
  151.         if(!empty($fields) && !empty($values)){
  152.           $q="INSERT INTO `$table` ($fields) VALUES ($values)";
  153.           $result = $this->prepared_query($q, $params);
  154.         }
  155.         if($result){
  156.             $id = $this->insert_id();
  157.             unset($result);
  158.             $this->free_result();
  159.             if($id) return $id;
  160.             else return true;
  161.         }
  162.         else return false;
  163.     }
  164.  
  165.     private function array_list($data,$segementCombine,$segmentValue,$implodeVal=', ')
  166.     {
  167.         $a = array_keys($data);
  168.         $b = $data;
  169.         $c = array_combine($a, $b);
  170.         foreach($c as $key=> $value){
  171.             $result[]=$key.$segementCombine.$segmentValue.$value.$segmentValue;
  172.         }
  173.         $updatefields= implode ($implodeVal, $result);
  174.         return $updatefields;
  175.     }
  176.    
  177.     function update($table, $data=null, $where_clause, $replacements=null){
  178.         $str2="";
  179.         $updatefields= $this->array_list($data,"=","'");
  180.         $Wherefields= $this->array_list($where_clause,"=","'");
  181.         if(!empty($where_clause))
  182.         {
  183.  
  184.             $where_clause="Where ".$Wherefields;
  185.         }
  186.         $QueryDump="update `$table` set ".$updatefields." ".$where_clause;     
  187.         $this->prepared_query($QueryDump);
  188.        
  189.         $affected_rows = $this->affected_rows();
  190.         $this->free_result();
  191.         return $affected_rows;
  192.     }
  193.    
  194.  
  195.     public function delete($table, $whereQuery){
  196.       $Wherefields= $this->array_list($whereQuery,"=","'");
  197.         if(!empty($whereQuery))
  198.         {
  199.             $whereQuery="Where ".$Wherefields;
  200.         }
  201.         $QueryDump="Delete from `$table` ".$whereQuery;
  202.        
  203.         $this->query($QueryDump);
  204.         $this->free_result();
  205.         return $this->affected_rows();
  206.        
  207.     }
  208.    
  209.    
  210.     function FieldOneRow($query=null, $replacements=null, $fetch_mode=null){
  211.         if(!$fetch_mode) $fetch_mode = $this->default_fetch_mode;
  212.         if($fetch_mode != 'object') $fetch_mode = 'assoc';
  213.         $func = "fetch_$fetch_mode";
  214.         if($query){
  215.           if($replacements)
  216.             $result = $this->prepared_query($query, $replacements);
  217.           else
  218.             $result = $this->query($query);
  219.           if(is_object($result)) $one = $result->$func();
  220.           else return $result;
  221.           unset($result);
  222.           $this->free_result();
  223.         return $one;
  224.         }return false;
  225.     }
  226.  
  227.   function FieldRow($query=null, $replacements=null, $mode=null){
  228.     return $this->FieldOneRow($query, $replacements, $mode);
  229.   }
  230.  
  231.  
  232.     function get($query=null, $replacements=null, $fetch_mode=null){
  233.       $rows = array();
  234.       if(!$fetch_mode) $fetch_mode = $this->default_fetch_mode;
  235.         if($fetch_mode != 'object') $fetch_mode = 'assoc';
  236.         $func = "fetch_$fetch_mode";
  237.         if($query){
  238.           if($replacements){
  239.             $func.='s';
  240.             $result = $this->prepared_query($query, $replacements);
  241.             if(is_object($result)) $rows = $result->$func();
  242.             else return $result;
  243.           }else{
  244.             $result = $this->query($query);
  245.             if(is_object($result)){
  246.                 while($row=$result->$func())
  247.                   $rows[]=$row;
  248.             }else return $result;
  249.           }
  250.       }
  251.       $this->free_result();
  252.         return $rows;
  253.     }
  254.  
  255.  
  256.     function ListColoums($colname, $query){
  257.       $tmp = array();
  258.         if($rows = $this->get($query, null, 'assoc'))
  259.             foreach($rows as $r)
  260.                 $tmp[] = $r[$colname];
  261.         return $tmp;
  262.     }
  263.    
  264.    
  265.     function GetOneRow($query=null, $replacements=null){
  266.       return $this->FieldOneRow($query, $replacements, "object");
  267.     }
  268.    
  269.     function GetAllRows($query=null, $replacements=null){
  270.       return $this->get($query, $replacements, "object");
  271.     }
  272.    
  273.    
  274.     function _escape_string($string){
  275.         return $this->real_escape_string(addslashes($string));
  276.     }
  277.    
  278.    
  279.     function clean($string){
  280.       return $this->_escape_string($string);
  281.     }
  282.    
  283.    
  284.     function closeMySQLiManager(){
  285.       unset($this->statementmgr);
  286.       $this->statementmgr = null;
  287.     }
  288.    
  289.    
  290.     function UnsetMySQLi(){
  291.       @$this->free_result();
  292.       $this->closeMySQLiManager();
  293.       unset($this->last_result);
  294.       $this->last_result = null;
  295.     }
  296.    
  297.     function __destruct(){
  298.       $this->UnsetMySQLi();
  299.       $this->close();    
  300.     }
  301.  
  302.    
  303.     public function prep($query){
  304.         $this->sql = $query;       
  305.     }
  306.  
  307.    
  308.     public function bind($hook, $value){
  309.         $this->dbBind[$hook] = $this->_escape_string($value);
  310.     }
  311.    
  312.    
  313.     public function run(){
  314.         if(empty($this->sql)) return false;
  315.         if(is_array($this->dbBind))
  316.         foreach($this->dbBind as $hook=>$value)
  317.         $this->sql = str_replace($hook, "'$value'", $this->sql);
  318.     $this->query($this->sql);
  319.     $this->dbBind = array();
  320.     $this->sql = "";
  321.     return $this->affected();
  322.     }
  323.    
  324.     public function affected(){
  325.       return $this->affected_rows();
  326.   }
  327.  
  328.    
  329.     public function db_insert_id(){
  330.         return $this->insert_id();
  331.     }
  332.    
  333.     public function iksquery($query){
  334.       return $this->query();
  335.     }
  336.    
  337.    
  338.   function Convert_Date_Time($phpdate=null){
  339.       if($phpdate) $phpdate = time();
  340.       return date( 'Y-m-d H:i:s', $phpdate );
  341.   }
  342.  
  343.  
  344.   function Convert_Unix_Time($mysqldate){
  345.       return strtotime($mysqldate);
  346.   }
  347.  
  348.  
  349.   function Pure_HTML($html, $textarea=0){
  350.      
  351.       if(!$textarea) $html = nl2br($html);
  352.    
  353.       $html = html_entity_decode($html);
  354.       return $html;
  355.   }
  356.    
  357. }
  358.  
  359. class MySQLiManager{
  360.   var $stmt;
  361.   protected $orig_query;
  362.   protected $query;
  363.   protected $params;
  364.   protected $param_indexes;
  365.   protected $typestring;
  366.   protected $fieldnames;
  367.   protected $assoc;
  368.  
  369.   function __construct(mysqli_stmt $stmt, $replace=null){
  370.     $this->stmt = $stmt;
  371.     $this->run_stmt($replace);
  372.   }
  373.  
  374.  
  375.   function run_stmt($replace=null){
  376.     if($replace and (is_array($replace) or is_object($replace))){
  377.       if(is_object($replace)) $replace = (array) $replace;
  378.       $this->params = $replace;
  379.       $this->bind_params();
  380.     }
  381.     $this->execute();
  382.   }
  383.  
  384.  
  385.   function bind_params(){
  386.     array_unshift($this->params, $this->getPreparedTypeString($this->params));
  387.     call_user_func_array(array($this->stmt, 'bind_param'), $this->refValues($this->params));
  388.   }
  389.  
  390.  
  391.   function refValues($arr){
  392.     if (strnatcmp(phpversion(),'5.3') >= 0){
  393.       $refs = array();
  394.       foreach($arr as $key => $value) $refs[$key] = &$arr[$key];
  395.       return $refs;
  396.     }
  397.     return $arr;
  398.   }
  399.  
  400.    
  401.     function getPreparedTypeString($saParams){
  402.     $sRetval = '';
  403.    
  404.     if ((!is_array($saParams) and !is_object($saParams)) or empty($saParams))
  405.       return $sRetval;
  406.    
  407.  
  408.     foreach ($saParams as $Param){
  409.       if (is_int($Param) && $Param < 2147483647) $sRetval .= 'i';
  410.       else if(is_double($Param) or is_float($Param) or (is_int($Param) and $Param >= 2147483647)) $sRetval .= 'd';
  411.       else if (is_string($Param)) $sRetval .= 's';
  412.       else $sRetval .= 'b';
  413.     }
  414.     $this->typestring = $sRetval;
  415.     return $sRetval;
  416.   }
  417.  
  418.   function execute(){
  419.     $this->free_result();
  420.     return $this->stmt->execute();
  421.   }
  422.  
  423.   function store_result(){
  424.     return $this->stmt->store_result();
  425.   }
  426.  
  427.   function free_result(){
  428.     return $this->stmt->free_result();
  429.   }
  430.  
  431.   function result_metadata(){
  432.     return $this->stmt->result_metadata();
  433.   }
  434.  
  435.  
  436.   function bind_result(){
  437.     unset($this->fieldnames, $this->assoc, $arr, $fieldnames);
  438.     $this->store_result();
  439.     $meta = $this->result_metadata();
  440.     if($meta){
  441.       $count = 1;
  442.       while($field = $meta->fetch_field()){
  443.         $fieldnames[$count] = &$arr[$field->name];
  444.         $count++;
  445.       }
  446.       call_user_func_array(array($this->stmt, "bind_result"), $fieldnames);
  447.     }else{
  448.       $fieldnames = false;
  449.       $arr = false;
  450.     }
  451.     $this->fieldnames =& $fieldnames;
  452.     $this->assoc =& $arr;
  453.     return $arr;
  454.   }
  455.  
  456.   function fetch(){
  457.     return $this->stmt->fetch();
  458.   }
  459.  
  460.  
  461.   function fetch_object(){
  462.     $this->bind_result();
  463.     $obj = $this->assoc;
  464.     if(!$this->fetch()) return false;
  465.     else return (object) $obj;
  466.   }
  467.  
  468.   function fetch_objects(){
  469.     $this->bind_result();
  470.     $arr = $this->assoc;
  471.     $copy = create_function('$a', 'return $a;');
  472.     $results = array();
  473.     while($this->fetch())
  474.       $results[] = (object) array_map($copy, $arr);
  475.     return $results;
  476.   }
  477.  
  478.  
  479.   function fetch_array(){
  480.     $this->bind_result();
  481.     $arr = $this->assoc;
  482.     if(!$this->fetch()) return false;
  483.     else return $arr;
  484.   }
  485.  
  486.  
  487.   function fetch_assoc(){
  488.     return $this->fetch_array();
  489.   }
  490.  
  491.  
  492.   function fetch_arrays(){
  493.     $this->bind_result();
  494.     $arr = $this->assoc;
  495.     $copy = create_function('$a', 'return $a;');
  496.     $results = array();
  497.     while($this->fetch()){
  498.       $results[] = array_map($copy, $arr);
  499.     }
  500.     return $results;
  501.   }
  502.  
  503.  
  504.   function fetch_assocs(){
  505.     return $this->fetch_arrays();
  506.   }
  507.  
  508.  
  509.   function affected_rows(){
  510.     return $this->stmt->affected_rows;
  511.   }
  512.  
  513.  
  514.   function insert_id(){
  515.     return $this->stmt->insert_id;
  516.   }
  517.  
  518.   function __destruct(){
  519.     @$this->stmt->free_result();
  520.     $this->stmt->close();
  521.   }
  522.  
  523. }
  524.  
  525. ?>
  526.  
  527. //EXAMPLE
  528. <?php
  529. //INCLUDE LIBRARY
  530. include("IKS_DB_MySqli.php");
  531. $p=new IKS_DB_MySqli('abcd','localhost','root','');
  532.  
  533. //INSERT DATA
  534. $data=array(
  535.     'n'=>'1',
  536.     );
  537. $p->insert('s',$data);
  538.  
  539. //FETCH DATA
  540. $sql="SELECT * From s";
  541. foreach($p->GetAllRows($sql) as $r)
  542. {
  543.     echo $r->ids."<br>";
  544. }
  545.  
  546. //UPDATE DATA
  547. $whereUpdate=array(
  548.     'ids'=>'10'
  549.     );
  550. $dataUpdate=array(
  551.     'n'=>'14',
  552.     );
  553. $p->update('s',$dataUpdate,$whereUpdate);
  554.  
  555. //DELETE DATA
  556. $whereDelete=array(
  557.     'ids'=>'10'
  558.     );
  559. $p->delete('s',$whereDelete);
  560. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement