zoukankan      html  css  js  c++  java
  • Codeigniter 使用 Mysql 存储过程

    本篇文章由:http://xinpure.com/codeigniter-using-mysql-stored-procedures/

    执行存储过程

    $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();

    如何获得多个结果集

    主要是使用 Mysqlimulti_query() 来获得

    关键代码如下:

    $mysqli = new mysqli('localhost', 'USERNAME', 'PASSWORD', 'DBNAME');
    $mysqli -> query("SET NAMES utf8");
    
    /* check connection */
    if (mysqli_connect_errno()) {
            printf('Connect failed: %s
    ', 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();
  • 相关阅读:
    POJ 3630
    Codeforces Round #219 (Div. 2) D题
    Codeforces Round #232 (Div. 2) On Sum of Fractions
    Codeforces Round #232 (Div. 2) C
    撸呀撸的左手(KMP+DP)
    hdu poj KMP简单题目总结
    LCT总结
    bzoj1019 [SHOI2008]汉诺塔
    NOIP2016总结
    p1199八数码问题
  • 原文地址:https://www.cnblogs.com/xinpureZhu/p/4538362.html
Copyright © 2011-2022 走看看