Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
503 views
in Technique[技术] by (71.8m points)

php - Trying to 'call' stored procedures with CodeIgniter

i've this working code with CI:

$this->db->query("call nameOfProcedure('param1', @param2)");
$query = $this->db->query('SELECT @param2 as results');
$row = $query->row();

it works, but if I try to use:

$this->db->call_function('nameOfProcedure', 'param1', '@param2');

i get the error:

This feature is not available for the database you are using.

What's wrong exactly?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Just in case it helps anyone. I use this library to work with stored procedures in CI, it supports multiple result sets too.

here is the code

I call it Mydb.php

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

class Mydb
{
   private $CI, $Data, $mysqli, $ResultSet;

   /**
   * The constructor
   */

   function __construct()
   {
     $this->CI =& get_instance();
     $this->Data = '';
     $this->ResultSet = array();
     $this->mysqli = $this->CI->db->conn_id;
   }

    public function GetMultiResults($SqlCommand)
    {
    /* execute multi query */
    if (mysqli_multi_query($this->mysqli, $SqlCommand)) {
        $i=0;
        do
        {

             if ($result = $this->mysqli->store_result()) 
             {
                while ($row = $result->fetch_assoc())
                {
                    $this->Data[$i][] = $row;
                }
                mysqli_free_result($result);
             }
            $i++; 
        }
        while ($this->mysqli->next_result());
    }
    return $this->Data;

   }   
}
?>  

call it like this from controller

$this->load->library('mydb');
$arr  = $this->mydb->GetMultiResults("CALL GetReferrals()");

Also, make sure to set mysqli the driver in application/config/database.php

$db['default']['dbdriver'] = 'mysqli';

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...