Example of using Mysql stored procedure in Codeigniter framework

Source: Internet
Author: User
Tags codeigniter

Execute the stored procedure

$ Query = $ this-> db-> query ('Call YOU_SP_NAME ');
$ Result = $ query-> result ();

This is not a big problem, just like executing a common SQL statement.

Problems encountered when using stored procedures

After the stored procedure is used, it is found that the database link cannot use the persistent link. set $ db ['pconnect '] in php to FALSE. Otherwise, the connection to the database fails.

In addition, after the stored procedure is executed, if other database queries are executed, the Commands out of sync; you can't run this command now error will occur.

The reason was found. It is said that the result set was not released after the stored procedure was executed.

CI can be solved by reconnecting to the database:

$ This-> db-> reconnect ();
How to obtain multiple result sets

It is mainly obtained using Mysqli multi_query ().

The key code is as follows:

$ Mysqli = new mysqli ('localhost', 'username', 'password', 'dbname ');
$ Mysqli-> query ("set names utf8 ");
/* Check connection */
If (mysqli_connect_errno ()){
Printf ('connect failed: % s \ n', 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 ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.