Advertisement
Guest User

St4t Class (Open for critique)

a guest
Apr 26th, 2013
1,516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.04 KB | None | 0 0
  1.     class st4t
  2.     {
  3.         private $prepared = false;
  4.         private $con;
  5.         private $statement;
  6.         private $query;
  7.         private $bind;
  8.         private $params;
  9.        
  10.         # Takes a mysqli object, an SQL-Query and an (optional) string specifying the types of data to be send (see mysqli_stmt::bind_param)
  11.         function __construct(&$con,$query,$bind = NULL)
  12.         {
  13.             $this->query = $query;
  14.             $this->bind = $bind;
  15.             $this->con = &$con;
  16.         }
  17.  
  18.         # Prepares and executes query with the values supplied to the function (read out by func_get_args() to allow for dynamic number of arguments)
  19.         public function execute()
  20.         {
  21.             # Prepare Statement if needed
  22.             if(!$this->prepared)
  23.             {
  24.                 if(!($this->statement = $this->con->prepare($this->query)))
  25.                 {
  26.                     throw new Exception('Failed to prepare query: '.$this->con->error);
  27.                 }
  28.  
  29.                 # Bind values if needed
  30.                 if(isset($this->bind))
  31.                 {
  32.                     # create $temp_binds array to pass to bind_param later on
  33.                     $temp_binds = array();
  34.                     $temp_binds[0]=$this->bind;
  35.                    
  36.                     # Assign the parameters by reference to the $this->params array, so we can change them easily later on.
  37.                     for($i = 0; $i < strlen($this->bind); $i++)
  38.                     {
  39.                         $temp_binds[]=&$this->params[$i];
  40.                     }
  41.  
  42.                     # Allow variable amounts of parameters
  43.                     call_user_func_array(array($this->statement, 'bind_param'),$temp_binds);
  44.                 }
  45.                
  46.                 $this->prepared = true;
  47.             }
  48.            
  49.             # Set values
  50.             if(isset($this->bind))
  51.             {
  52.                 # Check if the correct amount of arguments are supplied to the function
  53.                 if(strlen($this->bind)==func_num_args())
  54.                 {
  55.                     # Assign the arguments to the $this->params array so they get sent later
  56.                     foreach(func_get_args() as $key => $param)
  57.                     {
  58.                         $this->params[$key]=$param;
  59.                     }
  60.                 }
  61.                 else
  62.                 {
  63.                     throw new Exception('Failed to bind as number of params is not enough.');
  64.                 }
  65.             }
  66.            
  67.             # Actually execute the function and fetch/return results or true if nothing is to be fetched
  68.             if(!$this->statement->execute())
  69.             {
  70.                 throw new Exception('Failed to execute query: '.$this->statement->error);
  71.             }
  72.             else
  73.             {
  74.                 $result = array();
  75.                 $variables = array();
  76.                 $data = array();
  77.  
  78.                 # Fetch metadata to format our output properly
  79.                 $this->statement->store_result();
  80.                 $meta = $this->statement->result_metadata();
  81.                
  82.                 # Do we have returned data?
  83.                 if($meta)
  84.                 {
  85.                     # Bind result to $variables so we can read it out step by step
  86.                     while($field = $meta->fetch_field())
  87.                     {
  88.                         $variables[] = &$data[$field->name];
  89.                     }
  90.                     call_user_func_array(array($this->statement, 'bind_result'), $variables);
  91.  
  92.                     $i = 0;
  93.                    
  94.                     # Fetch statements and construct a new associative array for each in the form of [column-name=>column-value]
  95.                     while($this->statement->fetch())
  96.                     {
  97.                         $result[$i] = array();
  98.                         foreach($data as $k=>$v)
  99.                         {
  100.                             $result[$i][$k] = $v;
  101.                         }        
  102.                         $i++;
  103.                     }
  104.                    
  105.                     # Return array of all entries
  106.                     return $result;
  107.                 }
  108.                 else
  109.                 {
  110.                     return true;
  111.                 }
  112.             }
  113.         }
  114.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement