温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

php中怎么执行多个存储过程

发布时间:2021-07-09 17:06:28 来源:亿速云 阅读:107 作者:Leah 栏目:开发技术

今天就跟大家聊聊有关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();
}

看完上述内容,你们对php中怎么执行多个存储过程有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI