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