找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 284|回复: 0
打印 上一主题 下一主题

php执行多个存储过程的方法【基于thinkPHP】

[复制链接]

2560

主题

2560

帖子

7622

积分

论坛元老

Rank: 8Rank: 8

积分
7622
跳转到指定楼层
楼主
发表于 2018-2-14 05:31:01 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            本文实例讲述了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
";
$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程序设计有所帮助。
            
            
您可能感兴趣的文章:
  • php调用mysql存储过程
  • 用PHP调用Oracle存储过程的方法
  • PHP MSSQL 存储过程的方法
  • PHP得到mssql的存储过程的输出参数功能实现
  • php存储过程调用实例代码
  • PHP调用MsSQL Server 2012存储过程获取多结果集(包含output参数)的详解
  • ThinkPHP实现事务回滚示例代码
  • ThinkPHP多表联合查询的常用方法
  • PHP调用MySQL存储过程并返回值的方法
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端