Calling a stored procedure from CodeIgniter's Active Record class Calling a stored procedure from CodeIgniter's Active Record class codeigniter codeigniter

Calling a stored procedure from CodeIgniter's Active Record class


Yes , try this in your model.

$this->db->query("call {storedprocedure function name} ");

if you encounter trouble calling more than 1 stored procedure at a time you need to add the following line

/* ADD THIS FUNCTION IN SYSTEM/DATABASE/DB_ACTIVE_REC *//* USAGE $this->db->freeDBResource($this->db->conn_id); */function freeDBResource($dbh){    while(mysqli_next_result($dbh)){            if($l_result = mysqli_store_result($dbh)){              mysqli_free_result($l_result);            }        }}


If you are using later versions of codeigniter with mssql or sqlsrv with stored procedures, using 'CALL' as in query('CALL procedureName($param1,$params,....)') may not work.

In the case of MSSQL use:

$this->db->query('EXEC procedureName')

OR

$this->db->query('EXEC procedureName $param1 $param2 $param3,...')

In some cases you might need to turn on some constants for the driver. In this case run:

$this->db->query('Set MSSQL constant ON )

before running your regular query.


I have added the following function to class CI_DB_mysqli_driver in /system/database/drivers/mysqli/mysqli_driver.php

    function free_db_resource()    {        while(mysqli_next_result($this->conn_id))        {            if($l_result = mysqli_store_result($this->conn_id))            {                mysqli_free_result($l_result);            }        }    }

and use it after the procedure call

$this->db->free_db_resource();

Thanks to wework4web