重点:store_result,extra recordset

 

原文:http://www.php.net/manual/en/mysqli.store-result.php

参考http://www.php.net/manual/en/mysqli.multi-query.php

After reading through original notes and example above as well as wading through the documentation, I finally got a loop to work with two stored procedures.

Using the results of the first one as a parameter for the second one. Easier to do this way than a huge modified sequence of Inner Join queries.

Hope this helps others...

<?php
// Connect to server and database
$mysqli        = new mysqli("$dbServer", "$dbUser", "$dbPass", "$dbName");

// Open First Stored Procedure using MYSQLI_STORE_RESULT to retain for looping
$resultPicks = $mysqli->query("CALL $proc ($searchDate, $maxRSI, $incRSI, $minMACD, $minVol, $minTrades, $minClose, $maxClose)", MYSQLI_STORE_RESULT);

// process one row at a time from first SP
while($picksRow = $resultPicks->fetch_array(MYSQLI_ASSOC)) {
   
// Get Parameter for next SP
   
$symbol     = $picksRow['Symbol'];

   
// Free stored results
   
clearStoredResults($mysqli);

   
// Execute second SP using value from first as a parameter (MYSQLI_USE_RESULT and free result right away)
   
$resultData    = $mysqli->query("CALL prcGetLastMACDDatesBelowZero('$symbol', $searchDate)", MYSQLI_USE_RESULT);
   
$dataRow    = $resultData->fetch_array(MYSQLI_ASSOC);

   
// Dump result from both related queries
   
echo "<p>$symbol ... Num Dates: " . $dataRow['NumDates'];

   
// Free results from second SP
   
$resultData->free();

}

// Free results from first SP
$resultPicks->free();

// close connections
$mysqli->close();

#------------------------------------------
function clearStoredResults($mysqli_link){
#------------------------------------------
   
while($mysqli_link->next_result()){
      if(
$l_result = $mysqli_link->store_result()){
             
$l_result->free();
      }
    }
}
?>

Warner 21-Jul-2009 09:45
It also seems, that executing a SET statement in multi_query() returns an extra recordset too, which one would not expect.

lau at goldenweb dot com dot au 13-Apr-2007 08:29
Beware when using stored procedures:
If you connect to the database and then call dbproc A followed by a call to db proc B and then close the connection to the db, the second procedure call will not work.

It looks like there is a bug in MYSQL or mysqli that returns an extra recordset than you would expect. It then doesn't let you call another stored procedure until you finish processing all the recordsets from the first stored procedure call.

The solution is to simply loop through the additional recordsets between calls to db procs. Here is a function that I call between db proc calls:

<?php
#--------------------------------
function ClearRecordsets($p_Result){
#--------------------------------
   
$p_Result->free();   
    while(
$this->Mysqli->next_result()){
      if(
$l_result = $this->Mysqli->store_result()){
             
$l_result->free();
      }
    }
}

?>
posted on 2011-08-12 17:40  佐伊凡  阅读(1116)  评论(0编辑  收藏  举报