Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.14 KB | None | 0 0
  1. <?php
  2.  
  3.     #Made by Joopie
  4.     #Ver: 1.5
  5.  
  6.     defined('TRUEEVENT') or die();
  7.  
  8.     //Defining the new edited stmt class
  9.     class nMySQLi_stmt extends mysqli_stmt
  10.     {
  11.         //Defining needed variables
  12.         private $is_assoc = false;
  13.         private $row = array();
  14.  
  15.         //Execute the query and store the results etc
  16.         //Returns true/false
  17.         public function execute()
  18.         {
  19.             $return = parent::execute();
  20.  
  21.             $this->store_result();
  22.  
  23.             return $return;
  24.         }
  25.  
  26.         //Set a reference to get the values in a array
  27.         private function stmt_assoc(array &$out)
  28.         {
  29.             $data = $this->result_metadata();
  30.  
  31.             $fields = array();
  32.             $out = array();
  33.             while ($field = $data->fetch_field())
  34.             {
  35.                 $fields[] =& $out[$field->name];
  36.             }
  37.  
  38.             call_user_func_array(array($this, 'bind_result'), $fields);
  39.         }
  40.  
  41.         //Check if there is a row availible if so return it if not return false
  42.         public function next_record()
  43.         {
  44.             if (!$this->is_assoc)
  45.             {
  46.                 $this->is_assoc = true;
  47.  
  48.                 $this->stmt_assoc($this->row);
  49.             }
  50.  
  51.             if (!$this->fetch())
  52.             {
  53.                 $this->is_assoc = false;
  54.  
  55.                 $this->row = array();
  56.             }
  57.  
  58.             $data = array();
  59.             foreach ($this->row as $key => $value) //Dunno, still a good fix for references(y)
  60.             {
  61.                 $data[$key] = $value;
  62.             }
  63.  
  64.             return ($this->is_assoc) ? $data : false;
  65.         }
  66.  
  67.         //Returns the a value that you requested for, works the same as mysql_result()
  68.         public function result($field = null, $row = null)
  69.         {
  70.             $row = ($row == null) ? 0 : $row;
  71.             $field = ($field == null) ? 0 : $field;
  72.  
  73.             $this->data_seek($row);
  74.             $_row = $this->next_record();
  75.  
  76.             if ($field !== 0)
  77.             {
  78.                 $keys = array_keys($_row);
  79.  
  80.                 return $_row[$keys[$field]];
  81.             }
  82.  
  83.             return current($_row);
  84.         }
  85.  
  86.         //Close the shit Ghehe
  87.         function __destruct()
  88.         {
  89.             $this->close();
  90.         }
  91.     }
  92.  
  93.     //Defining the new mysqli class
  94.     class nMySQLi extends MySQLi
  95.     {
  96.         //Defining instance
  97.         private static $instance = null;
  98.    
  99.         //Defining needed variables
  100.         private $stmt = null;
  101.         private $override = array( //The functions we override
  102.                     'old_stmt_init' => 'stmt_init',
  103.                     'old_prepare' => 'prepare',
  104.                     'old_bind_param' => 'bind_param',
  105.                     'old_execute' => 'execute'
  106.                 );
  107.  
  108.         //Count the queries that's been executed.
  109.         public static $count = 0;
  110.         public static $queries = array();
  111.        
  112.         public static function GetMySQLi($hostname = null, $username = null, $password = null, $database = null)
  113.         {
  114.             if (!isset(self::$instance))
  115.             {
  116.                 self::$instance = new self($hostname, $username, $password, $database);
  117.             }
  118.            
  119.             return self::$instance;
  120.         }
  121.        
  122.         //Returns if connected
  123.         public function connected()
  124.         {
  125.             return $this->connect_errno === 0;
  126.         }
  127.  
  128.         //Disconnect it
  129.         public function disconnect()
  130.         {
  131.             return $this->close();
  132.         }
  133.        
  134.         //Preform prepared query
  135.         public function doquery($sql)
  136.         {
  137.             $args = func_get_args();
  138.            
  139.             call_user_func_array(array($this, 'prepare'), $args);
  140.            
  141.             return $this->execute();
  142.         }
  143.        
  144.         //Construct new stmt class
  145.         public function stmt_init() //Overrided
  146.         {
  147.             return new nmysqli_stmt($this);
  148.         }
  149.  
  150.         //Prepare the new query and returns this
  151.         public function prepare($sql) //Overrided
  152.         {
  153.             //argument parser
  154.             $args = func_get_args();
  155.             $sql = array_shift($args);
  156.            
  157.             $this->stmt = $this->stmt_init();
  158.  
  159.             if (!$this->stmt->prepare($sql))
  160.             {
  161.                 throw new EventException($this->stmt->error, $this->stmt->errno);
  162.             }
  163.  
  164.             self::$count++;
  165.             self::$queries[] = $sql;
  166.            
  167.             //parse params
  168.             if (func_num_args() > 1)
  169.             {
  170.                 call_user_func_array(array($this, 'bind_params'), $args);
  171.             }
  172.            
  173.             return $this;
  174.         }
  175.  
  176.         //Gets the type
  177.         public static function gettype($value)
  178.         {  
  179.             if (is_array($value)) return 'a';
  180.             if (is_bool($value)) return 'b';
  181.             if (is_double($value)) return 'd';
  182.             if (is_string($value)) return 's';
  183.             if (is_numeric($value)) return 'i';
  184.  
  185.             return 'u';
  186.         }
  187.  
  188.         //Bind params fix for 5.3 or higher
  189.         private function getrightparams(array &$array, array &$out)
  190.         {
  191.             if (strnatcmp(phpversion(),'5.3') >= 0)
  192.             {
  193.                 foreach ($array as $key => $value)
  194.                 {
  195.                     $out[] =& $array[$key];
  196.                 }
  197.             }
  198.             else
  199.             {
  200.                 $out = $array;
  201.             }
  202.         }
  203.  
  204.         //Bind the params to the stmt
  205.         public function bind_params() //Overrided
  206.         {
  207.             $args = func_get_args();
  208.             $paramTypes = '';
  209.  
  210.             foreach ($args as $value)
  211.             {
  212.                 $paramTypes .= self::gettype($value);
  213.             }
  214.  
  215.             $params = array($paramTypes);
  216.             $this->getrightparams($args, $params);
  217.  
  218.             call_user_func_array(array($this->stmt, 'bind_param'), $params);
  219.  
  220.             return $this;
  221.         }
  222.  
  223.         //Execute the query
  224.         public function execute() //Overrided
  225.         {
  226.             if (!$this->stmt->execute())
  227.             {              
  228.                 throw new EventException($this->stmt->error, $this->stmt->errno);
  229.             }
  230.  
  231.             return $this->stmt;
  232.         }
  233.  
  234.         //Call the old function of the MySQLi class without the new one
  235.         //Returns the normaly result of that function
  236.         //False on error
  237.         function __call($method, $arguments)
  238.         {
  239.             if (in_array($method, array_keys($this->override)))
  240.             {
  241.                 return call_user_func_array(array(parent, $this->override[$method]), $arguments);
  242.             }
  243.  
  244.             return null;
  245.         }
  246.     }
  247.  
  248. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement