Link: http://imysql.com/node/298
I have previously written about how to obtain MySQL procedure results through PHP. However, this method cannot adapt to multiple result sets. A multi-result set refers to multiple SELECT queries and multiple result sets in the itinerary in the stored procedure. The solution is to use the mysqli_multi_query () function provided by mysqli, as shown in the following example:
$mysqli = new mysqli("localhost", "root", "", "test");if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit();}$query = "call yejr();";$mysqli->multi_query($query);while($mysqli->more_results()){ if ($result = $mysqli->store_result()) { while ($row = $result->fetch_row()) { //printf("%s\n", $row[0]); print_r($row); } $result->close(); } $mysqli->next_result();}
This example can be found in the standard php manual mysqli. I just want to make some modifications.
Method 2: Use PDO to obtain
try { $dbh = new PDO('mysql:host=localhost;dbname=test', 'root', ''); $stmt = $dbh->query("call yejr();"); do { $rows = $stmt->fetchAll(PDO::FETCH_NUM); if ($rows) { print_r($rows); } } while ($stmt->nextRowset()); $dbh = null;} catch (PDOException $e) { print "Error!: " . $e->getMessage() . ""; die();}
An example of a stored procedure is as follows:
CREATE PROCEDURE `yejr`()beginselect * from yejr limit 1;select * from yejr limit 2;end;