Php PDO calls the mysql Stored Procedure of multiple result sets

Source: Internet
Author: User

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;

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.