How do you retrieve multiple result sets from a stored procedure in PHP/mysqli?

DDD
Release: 2024-11-01 18:45:02
Original
781 people have browsed it

How do you retrieve multiple result sets from a stored procedure in PHP/mysqli?

Retrieving Multiple Result Sets with Stored Procedures in PHP/mysqli

In PHP/MySQLi, executing a stored procedure with multiple result sets requires careful handling. To advance to the second result set after executing the stored procedure, you must:

  1. Use mysqli_stmt_execute: Execute the stored procedure using mysqli_stmt_execute.
  2. Fetch the first result set: Call mysqli_stmt_get_result to obtain the first result set and read its data using fetch_assoc, fetch_array, or fetch_row.
  3. Move to the next result set: After reading the first result set, call mysqli_stmt_next_result to move to the next result set.
  4. Fetch the second result set: Call mysqli_stmt_get_result again to obtain the second result set and read its data.

Here's an example code using PHP/MySQLi:

<code class="php">$stmt = mysqli_prepare($db, 'CALL multiples(?, ?)');
mysqli_stmt_bind_param($stmt, 'ii', $param1, $param2);
mysqli_stmt_execute($stmt);

// Fetch the first result set
$result1 = mysqli_stmt_get_result($stmt);
while ($row = $result1->fetch_assoc()) {
    printf("%d\n", $row['id']);
}

// Move to the second result set
mysqli_stmt_next_result($stmt);
$result2 = mysqli_stmt_get_result($stmt);
while ($row = $result2->fetch_assoc()) {
    printf("%d\n", $row['id']);
}

mysqli_stmt_close($stmt);</code>
Copy after login

This code successfully retrieves and prints the data from both result sets in the specified stored procedure.

The above is the detailed content of How do you retrieve multiple result sets from a stored procedure in PHP/mysqli?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!