Welcome Guest, Not a member yet? Register   Sign In
MySQLi Library with Stored Procedures & Multiple result sets support
#1

[eluser]Atasa[/eluser]
I was very anxious every time a new release of Ci was coming out to see weather if
mysqli driver was more enhanced and complete, and was always tweaking the mysqli_driver to support
the projects of mine that use Stored Procedures, and sometimes wanted to have Multiple Result Sets in one go, instead of writing 2 or more sql queries with the same pattern.
Maybe I am too lazy but I am happy about it.
Therefor one day I decided to write a library to do this and not worry any more about CI system files.

So here it is:

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Mydb
{
    private $CI, $Data, $mysqli;
    
    /**
      * The constructor
    */
    public function __construct()
    {
        $this->CI = & get_instance();
        $this->Data = array();
        $this->mysqli = $this->CI->db->conn_id;
    }
    
    public function Query($sql, $mode="Array")
    {
        $result = @$this->mysqli->query($sql); //assign results to $result.
         switch ($mode)
           {
             case 'Row':
              //use the data in the resultset
              $this->Data = $result->fetch_object();

              break;                            
                        
              default:
                while ($row = $result->fetch_object())
                 {
                   $this->Data[] = $row;                        
                 }
               break;
           }
            
            //free the resultset
            $result->free();
          
            //clear the other result(s) from buffer loop through each result using the next_result() method
            while ($this->mysqli->next_result())
            {
                //free each result.
                $result = $this->mysqli->use_result();
                if ($result instanceof mysqli_result)
                {
                    $result->free();
                }
            }
         return $this->Data;
    }
    
    public function Multi_Query($sql)
    {
       // automatically buffers resultsets and assigns true or false on fail to $query
       $query = @$mysqli->multi_query($sql);
        //$this->query = $sql;
         do
         {
            /* store first result set */
            if ($result = $this->mysqli->store_result())
            {
                $this->Data[] = $result->fetch_object();
                $result->free();
            }
          }
          while ($this->mysqli->next_result());
        # DEBUG   print $sql;
        return $this->Data;
    }          
}
/* End of file Mydb.php */

/* Location: ./system/application/libraries/Mydb.php */


Messages In This Thread
MySQLi Library with Stored Procedures & Multiple result sets support - by El Forum - 11-11-2008, 04:39 AM



Theme © iAndrew 2016 - Forum software by © MyBB