在codeigniter中调用存储过程
我正在使用最新的 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 的核心?
I am using latest codeigniter and trying to call stored procedure from my model. Also I am using mysqli as database driver. Now I am having an error when I call two stored procedures. Following is the error:
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
Note that when I call a single stored procedure it works fine. Here is the code for model.
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();
}
}
Here is the code from controller
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();
}
}
Is there any solution without hacking the core of codeigniter??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我关注 Tim Brownlaw 先生的博客:
http://ellislab.com/forums/viewthread/73714/#562711
首先,修改 application/config/config.php,第 55 行。
然后,将以下内容添加到 mysqli_result.php 中,由于某些奇怪的原因缺少此命令(在/system/database/drivers/mysqli/mysqli_result.php)。
然后,在您的模型中添加
$result->next_result()
。下面是我的例子。
I follow the blog of Mr. Tim Brownlaw:
http://ellislab.com/forums/viewthread/73714/#562711
First, modify application/config/config.php, line 55.
Then, add the following into mysqli_result.php that is missing this command for some strange reason (under /system/database/drivers/mysqli/mysqli_result.php).
Then, in your model, add
$result->next_result()
.Below is my example.
遇到同样的问题,我找到了另一种方法,它不改变核心,而是使用一个小助手。
编辑:找不到以下链接的资产。
请参阅 CoreyLoose 帖子。https://ellislab.com /forums/viewthread/71141/#663206不过,我不得不对他的助手进行一些小小的调整。该行
可能会产生警告,因为 $result 有时作为布尔值传递。我只是在这条线之前进行了检查,现在它工作得很好,没有修改核心!
Having the same problem I found another approach which doesn't change the core, but instead uses a small helper.
Edit: The below linked asset is nowhere to be found.
See CoreyLoose post.https://ellislab.com/forums/viewthread/71141/#663206I had to make a small adjusment to his helper though. The line
could possibly produce a warning since the $result sometimes is passed as a boolean. I just put a check prior to this line and now it works perfectly, with no tinkering with the core!
这似乎是 CodeIgniter 中的一个错误。为什么它还在那里,我无法理解。
然而,有几种方法可以克服它。
检查此处:http://codeigniter.com/forums/viewthread/73714/
基本上,您修改 mysqli_result.php 以包含 next_result() 函数,并确保在每个存储过程之后调用它。称呼。
请注意,它假设您使用 mysqli 作为数据库驱动程序...但您可能可以对任何其他驱动程序执行类似的操作。您可以在 /application/config/database.php 中更改驱动程序,这行是这样说的
默认情况下。将其更改为:
您也可以关闭/重新打开调用之间的数据库连接,但我绝对建议不要采用这种方法。
This seems to be a bug in CodeIgniter. How come it's still in there is beyond me.
However, there's a couple of ways to overcome it.
Check here: http://codeigniter.com/forums/viewthread/73714/
Basically, you modify mysqli_result.php to include next_result() function and make sure to call it after every stored proc. call.
Just note that it assumes you're using mysqli as your DB driver... but you can probably do something similar with any other. You can change your driver in /application/config/database.php It's the line that says
by default. Change it to:
You could also just close/reopen a DB connection between the calls, but I would definitely advise against that approach.
将 dbdriver 更改为“mysqli”
将此函数放入您的模型中并使用它来调用存储过程
change dbdriver to "mysqli"
put this function to your model and use it to call stored procedure