【问题标题】:Calling stored procedure in codeigniter在codeigniter中调用存储过程
【发布时间】:2011-12-07 17:01:26
【问题描述】:

我正在使用最新的 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核心的解决方案??

【问题讨论】:

    标签: mysql codeigniter stored-procedures codeigniter-2


    【解决方案1】:

    我关注 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;
    }
    

    【讨论】:

    • 可以用pdo驱动模拟吗?
    【解决方案2】:

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

    编辑:找不到以下链接的资产。

    查看 CoreyLoose 的帖子。

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

    不过,我不得不对他的助手做一个小小的调整。线

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

    可能会产生警告,因为 $result 有时会作为布尔值传递。我只是在这条线之前打了个勾,现在它可以完美运行,无需修改核心!

    【讨论】:

    • 这是迄今为止侵入性最小的方法,因为它不需要更改 CI 核心。我也用过这个。
    • 你能把代码贴在这里吗?即使我已经有一个 ellislab 帐户,我也无法访问该链接。谢谢。
    • 能否请您更新参考或在此处提供完整的解决方案,因为链接不起作用。非常感谢。
    • 对不起,这是 6 年前的事了,我不知道这段时间 codeigniter 发生了什么。
    【解决方案3】:

    这似乎是 CodeIgniter 中的一个错误。它怎么还在里面,我无法理解。 但是,有几种方法可以克服它。

    在这里查看:http://codeigniter.com/forums/viewthread/73714/ 基本上,您修改 mysqli_result.php 以包含 next_result() 函数,并确保在每个存储过程之后调用它。称呼。 请注意,它假设您使用 mysqli 作为您的数据库驱动程序......但您可能可以对任何其他人做类似的事情。您可以在 /application/config/database.php 中更改您的驱动程序 这是说

    的行

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

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

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

    您也可以在调用之间关闭/重新打开数据库连接,但我绝对建议不要使用这种方法。

    【讨论】:

    • 谢谢Veggen,现在看来不破解codeigniter的核心就没有办法使用了?
    • 好吧,除了关闭和重新打开调用之间的数据库连接之外,不,似乎没有办法。但是,如果有任何吊唁的话,所需的改变是非常小的。我正在我当前的项目中使用这样的 hack :(
    • 好的。只是想确认一下。无论如何,谢谢,干得好;)
    • 考虑将接受的答案更改为 jonas',因为这似乎是首选方式。
    【解决方案4】:

    将 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;
    
    }
    

    【讨论】:

      猜你喜欢
      • 2015-11-11
      • 2013-08-14
      • 1970-01-01
      • 2016-01-11
      • 2023-04-04
      • 2013-11-23
      • 2012-02-21
      相关资源
      最近更新 更多