zoukankan      html  css  js  c++  java
  • php执行多个存储过程

    2014年3月18日更新:

    从以前的使用原生代码来看,只需要将结果集关闭即可,即 $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,已经对存储过程进行了一定处理,但是不知道为什么运行不了。

    用原生代码解决了问题(下面是部分代码):

     1 $db = new mysqli(C("DB_HOST"), C("DB_USER"), C("DB_PWD"), C("DB_NAME"));
     2 if (mysqli_connect_errno())
     3 throw_exception(mysqli_connect_error());
     4 
     5 $t2 = microtime(true);
     6 echo "数据库连接用时:" . (($t2 - $t1)) . "s <br />";
     7 
     8 $arr = array();
     9 // 1st Query
    10 $procedure = "call p1()";
    11 $result = $db->query($procedure);
    12 if ($result) {
    13 // Cycle through results
    14 while ($row = $result->fetch_object()) {
    15 //添加到对象数组
    16 $arr[] = $row;
    17 }
    18 // 这里是最重要的,需要将游标移动下一个结果集
    19 $result->close();
    20 $db->next_result();
    21 }
    22 $procedure = "call p2()";
    23 $result = $db->query($procedure);
    24 if ($result) {
    25 // Cycle through results
    26 while ($row = $result->fetch_object()) {
    27 //添加到对象数组
    28 $arr[] = $row;
    29 }
    30 // 这里是最重要的,需要将游标移动下一个结果集
    31 $result->close();
    32 $db->next_result();
    33 }
  • 相关阅读:
    Django框架之视图层
    Django框架之模型层 多表操作
    Django框架之模型层 单表操作
    Django框架之模板层
    Django框架之路由层
    Django框架及ORM的基本使用
    Django框架安装
    python web的三大主流框架
    web基础之手动实现简易web服务框架
    C# 语法特性
  • 原文地址:https://www.cnblogs.com/trying/p/3169986.html
Copyright © 2011-2022 走看看