2011-10-14 30 views
8

我正在使用最新的codeigniter並嘗試從我的模型調用存儲過程。另外我使用mysqli作爲數據庫驅動程序。現在,當我調用兩個存儲過程時發生錯誤。以下是錯誤:在codeigniter中調用存儲過程

Error Number: 2014

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

call uspTest();

Filename: E:\wamp\www\reonomy-dev\system\database\DB_driver.php

Line Number: 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(); 
} 

} 

是否有沒有黑客笨的核心的任何溶液??

回答

5

這似乎是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';

你也只是關閉/重新調用之間的數據庫連接,但我肯定會建議對這一做法。

+0

感謝Veggen,現在看來是沒有辦法使用它沒有黑客codeigniter的核心? –

+0

那麼,除了關閉和重新打開通話之間的數據庫連接,否,似乎沒有辦法。但是,如果有任何吊,,所需的改變是非常小的。我在我目前的項目中使用了這樣的黑客攻擊:( – kaqqao

+0

O.K.只是想確認,不管怎樣,感謝和尼斯的工作;) –

7

遇到同樣的問題,我找到了另一種不改變核心的方法,而是使用了一個小幫手。

編輯:無法找到下面的鏈接資源。

查看CoreyLoose的帖子。

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

我不得不做出一個小adjusment他的幫手,雖然。該行

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

可能會產生警告,因爲$ result有時會作爲布爾值傳遞。我只是在這條線之前進行檢查,現在它完美地工作,沒有修補核心!

+0

這看起來不錯。必須嘗試。 – kaqqao

+0

這是迄今爲止最沒有侵入性的方法,因爲它不需要更改CI核心。我也使用過這個。 –

+0

非常感謝jonas – Drew

17

我跟着添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; 
} 
+0

是否可以模擬與pdo驅動程序相同的內容? – Dimas

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; 

}