本文实例讲述了php执行多个存储过程的方法。分享给大家供大家参考,具体如下:
从以前的使用原生代码来看,只需要将结果集关闭即可,即
$this -> queryid -> close();
使用mysqli方式,修改dbmysqli.class.php,将query函数改为:
public function query($str) {
$this -> initconnect(false);
if (!$this -> _linkid) {
return false;
}
$this -> querystr = $str;
//释放前次的查询结果
if ($this -> queryid)
$this -> free();
n('db_query', 1);
// 记录开始执行时间
g('querystarttime');
$this -> queryid = $this -> _linkid -> query($str);
// 对存储过程改进
$ret = array();
$this -> debug();
if (false === $this -> queryid) {
$this -> error();
return false;
} else {
$this -> numrows = $this -> queryid -> num_rows;
$this -> numcols = $this -> queryid -> field_count;
$ret = $this -> getall();
}
//主要将这段移动了一下,关闭结果集
if ($this -> _linkid -> more_results()) {
while (($res = $this -> _linkid -> next_result()) != null) {
$this -> queryid -> close();
}
}
return $ret ;
}
下面就可以调用多个存储过程,或许执行其他sql操作,可以直接使用m函数
在使用thinkphp的时候发现执行多个存储过程只能执行第一个,看了一下源码driver/db/dbmysql.class,已经对存储过程进行了一定处理,但是不知道为什么运行不了。
用原生代码解决了问题(下面是部分代码):
$db = new mysqli(c("db_host"), c("db_user"), c("db_pwd"), c("db_name"));
if (mysqli_connect_errno())
throw_exception(mysqli_connect_error());
$t2 = microtime(true);
echo "数据库连接用时:" . (($t2 - $t1)) . "s <br />";
$arr = array();
// 1st query
$procedure = "call p1()";
$result = $db->query($procedure);
if ($result) {
// cycle through results
while ($row = $result->fetch_object()) {
//添加到对象数组
$arr[] = $row;
}
// 这里是最重要的,需要将游标移动下一个结果集
$result->close();
$db->next_result();
}
$procedure = "call p2()";
$result = $db->query($procedure);
if ($result) {
// cycle through results
while ($row = $result->fetch_object()) {
//添加到对象数组
$arr[] = $row;
}
// 这里是最重要的,需要将游标移动下一个结果集
$result->close();
$db->next_result();
}
更多关于thinkphp相关内容感兴趣的读者可查看本站专题:《thinkphp入门教程》、《thinkphp模板操作技巧总结》、《thinkphp常用方法总结》、《smarty模板入门基础教程》及《php模板技术总结》。
希望本文所述对大家基于thinkphp框架的php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!