Calling a stored procedure from codeigniter

I am using codeigniter, which has mysqli as the db driver, I am trying to call a simple stored procedure from my model, but I get an error. What am I doing wrong

Error Number: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'pc()' at line 1 pc() Filename: C:\hosted\saner.gy\ipa\system\database\DB_driver.php Line Number: 330 

When I run the request call to the Stored Procedure, it works fine, but from codeigniter it throws the above error

Stored procedure

 CREATE DEFINER=`root`@`localhost` PROCEDURE `pc`() LANGUAGE SQL NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER COMMENT '' BEGIN SELECT * FROM tbl_flo WHERE name = 'sam'; END 

controller

 public function sp() { $this->User_model->pc(); } 

Model

 public function pc() { $query = $this->db->query("pc()"); return $query->result(); } 
+4
source share
3 answers

Stored procedures are called using a CALL procedure_name(optional_params) request CALL procedure_name(optional_params) .

You need to edit the query used in your model as follows:

 public function pc() { $query = $this->db->query("CALL pc()"); return $query->result(); } 
+2
source

You use the following method of calling a procedure.

 $this->db->call_function('pc'); 

Or you can also use this

 $this->db->query("call pc()"); 
+2
source

Here dude, this code block works for me in the model

 function get_sunmeter_for_initiator($data){ try { $this->db->reconnect(); $sql = "CALL `get_sunmeter_for_initiator`(?, ?, ?)"; $result = $this->db->query($sql,$data); // $data included 3 param and binding & query to db $this->db->close(); } catch (Exception $e) { echo $e->getMessage(); } return $result; } 
+2
source

All Articles