codeigniter mysql 存储过程_Codeigniter框架使用Mysql存储过程的例子

执行存储过程

$query  = $this -> db -> query('CALL YOU_SP_NAME');

$result = $query -> result();

这个问题不大,就像是执行普通 SQL 语句一样。

使用存储过程遇到的问题

在用了存储过程之后,发现数据库链接并不能使用 持续链接,即 需要把 database.php 里的 $db['pconnect'] 设置为 FALSE,否则会出现链接数据库失败的错误。

另外,当执行完存储过程之后,如果再执行其它的数据库查询,会出现 Commands out of sync; you can't run this command now 错误。

查了下原因,据说是因为执行完存储过程后,没有将结果集给释放掉

CI 中可以通过重连数据库的方法解决:

$this -> db -> reconnect();

如何获得多个结果集

主要是使用 Mysqli 的 multi_query() 来获得

关键代码如下:

$mysqli = new mysqli('localhost', 'USERNAME', 'PASSWORD', 'DBNAME');

$mysqli -> query("SET NAMES utf8");

/* check connection */

if (mysqli_connect_errno()) {

printf('Connect failed: %s\n', mysqli_connect_error());

exit();

}

$query  = 'CALL YOU_SP_NAME';

/* execute multi query */

if ($mysqli -> multi_query($query)) {

do {

/* store first result set */

if ($result = $mysqli -> store_result()) {

while ($row = $result -> fetch_all()) {

$all_result[] = $row;

}

$result -> free();

}

}

while ($mysqli -> next_result());

}

/* close connection */

$mysqli -> close();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值