Advertisement
eyuprog

mysqlminang

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