CodeIgniter MySQL Stored Procedure and Function

MySQL has procedure and function feature. There is good article explaining both topics in http://bit.ly/dWDh9w.

To call mysql function from codeigniter, we can simply use query() function from database class. For example

$query = $this->db->query("call storedFunction($param1, $param2) as A");
$row = $query->row();
echo $row->A;

Where storedFunction is the name of function we have created before.

To call mysql stored procedure from codeigniter, we can use query() function from database class. For example

$query = "CALL hello('Chandra', @a)";
$res = $this->db->query($query);
$query = "SELECT @a as A";
$res = $this->db->query($query);
$row = $res->row();

Where hello is the name of stored procedure.

Hope this article helps 🙂

6 thoughts on “CodeIgniter MySQL Stored Procedure and Function”

  1. incase of ODBC connection use the following code to call the stored procedure:
    $query=$this->db->query(“{call usp_Students}”);
    return $query->result();

  2. Hi, any idea how to call from Codeigniter a MYSQL stored procedure that produces multiple record sets? (for example a procedure that executes 10 separate SELECT queries and produces 10 separate recordsets)? I get back only the record set of the first SELECT statement of the procedure. I checked dozens of articles on the net so far nothing works. Thanks for any help!

  3. Error Number: 2014

    Commands out of sync; you can’t run this command now

    I am getting this error

Leave a Reply

Your email address will not be published. Required fields are marked *