Php Method for executing multiple stored procedures [based on thinkPHP], stored procedure thinkphp

Source: Internet
Author: User

Php Method for executing multiple stored procedures [based on thinkPHP], stored procedure thinkphp

This example describes how php executes multiple stored procedures. We will share this with you for your reference. The details are as follows:

From the previous use of native code, you only need to disable the result set, that is

$this -> queryID -> close();

In mysqli mode, modify DbMysqli. class. php and change the query function:

Public function query ($ str) {$ this-> initConnect (false); if (! $ This-> _ linkID) {return false;} $ this-> queryStr = $ str; // release the previous query result if ($ this-> queryID) $ this-> free (); N ('db _ query', 1); // record start execution time G ('querystarttime '); $ this-> queryID = $ this-> _ linkID-> query ($ str); // improves the Stored Procedure $ 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 ();} // this section is moved, close the result set if ($ this-> _ linkID-> more_results () {while ($ res = $ this-> _ linkID-> next_result ())! = NULL) {$ this-> queryID-> close () ;}return $ ret ;}

You can call multiple stored procedures, or execute other SQL operations. You can directly use the M function.

When using thinkphp, you can only execute the first one when executing multiple stored procedures. Let's take a look at the source code Driver/Db/DbMysql. class, the stored procedure has been processed, but I don't know why it cannot be run.

Solved the problem with the native code (below is some code ):

$ 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 "database connection time :". ($ t2-$ t1 )). "s <br/>"; $ arr = array (); // 1st Query $ procedure = "call p1 ()"; $ result = $ db-> query ($ procedure); if ($ result) {// Cycle through resultswhile ($ row = $ result-> fetch_object ()) {// Add to the object array $ arr [] = $ row;} // This is the most important. You need to move the cursor to the next result set $ result-> close (); $ db-> next_result () ;}$ procedure = "call p2 ()"; $ result = $ db-> query ($ procedure); if ($ result) {// Cycle through resultswhile ($ row = $ result-> fetch_object () {// Add to object array $ arr [] = $ row ;} // here is the most important. You need to move the cursor to the next result set $ result-> close (); $ db-> next_result ();}

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.