在codeigniter中调用存储过程

时间:2011-10-14 11:56:20

标签: mysql codeigniter stored-procedures codeigniter-2

我正在使用最新的codeigniter并尝试从我的模型中调用存储过程。我也使用mysqli作为数据库驱动程序。现在我在调用两个存储过程时遇到错误。以下是错误:

  

错误号码:2014

     

命令不同步;你现在不能运行这个命令

     

调用uspTest();

     

文件名:E:\ wamp \ www \ reonomy-dev \ system \ database \ DB_driver.php

     

行号:330

请注意,当我调用单个存储过程时,它可以正常工作。这是模型的代码。

class Menus_model extends CI_Model {

function __construct()
{
    parent::__construct();

}

public function getMenus()
{
    $query = $this->db->query("call uspGetMenus()");

    return $query->result();
}

public function getSubMenus()
{
    $query = $this->db->query("call uspTest()");
    return $query->result();
}

}

以下是控制器的代码

class MYHQ extends CI_Controller {

public function __construct()
{
    parent::__construct();
    $this->load->model('menus_model');
}

public function index()
{
    $menu = $this->menus_model->getMenus();
    $submenu = $this->menus_model->getSubMenus();
}

}

是否有任何解决方案没有破解codeigniter的核心?

4 个答案:

答案 0 :(得分:20)

我跟随Tim Brownlaw先生的博客:
http://ellislab.com/forums/viewthread/73714/#562711

首先,修改application / config / config.php,第55行。

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

然后,将以下内容添加到缺少此命令的mysqli_result.php中,原因有些奇怪(在/system/database/drivers/mysqli/mysqli_result.php下)。

/**
  * Read the next result
  *
  * @return  null
  */   
 function next_result()
 {
     if (is_object($this->conn_id))
     {
         return mysqli_next_result($this->conn_id);
     }
 }

然后,在您的模型中添加$result->next_result()

以下是我的例子。

function list_sample($str_where, $str_order, $str_limit)
{
   $qry_res    = $this->db->query("CALL rt_sample_list('{$str_where}', '{$str_order}', '{$str_limit}');");

   $res        = $qry_res->result();

   $qry_res->next_result(); // Dump the extra resultset.
   $qry_res->free_result(); // Does what it says.

   return $res;
}

答案 1 :(得分:7)

遇到同样的问题我发现了另一种不改变核心的方法,而是使用了一个小帮手。

修改:无法找到以下链接的资源。

参见CoreyLoose帖子。

https://ellislab.com/forums/viewthread/71141/#663206

我不得不对他的助手做出一个小调整。这条线

if( get_class($result) == 'mysqli_stmt' )

可能会产生警告,因为$ result有时会作为布尔值传递。我只是在这一行之前做了一个检查,现在它完美无缺,没有修补核心!

答案 2 :(得分:5)

这似乎是CodeIgniter中的一个错误。为什么它仍然存在于我之外。 但是,有几种方法可以克服它。

点击此处:http://codeigniter.com/forums/viewthread/73714/ 基本上,你修改mysqli_result.php以包含next_result()函数,并确保在每个存储的proc之后调用它。呼叫。 请注意,它假设您使用mysqli作为您的数据库驱动程序......但您可以执行与其他任何类似的操作。您可以在/application/config/database.php中更改驱动程序。这是

  

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

默认情况下。将其更改为:

  

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

您也可以在呼叫之间关闭/重新打开数据库连接,但我肯定会反对这种方法。

答案 3 :(得分:2)

将dbdriver更改为" mysqli" 将此函数放入模型并使用它来调用存储过程

function call( $procedure )
{
    $result = @$this->db->conn_id->query( $procedure );

    while ( $this->db->conn_id->next_result() )
    {
        //free each result.
        $not_used_result = $this->db->conn_id->use_result();

        if ( $not_used_result instanceof mysqli_result )
        {
            $not_used_result->free();
        }
    }

    return $result;

}
相关问题