使用 MySQLi 从存储过程中检索多个结果集
您的存储过程“multiples”会生成多个结果集。要前进到后续结果集并从中检索数据,请使用 MySQLi 扩展执行以下步骤:
PHP 过程样式:
<code class="php">// Prepare the statement $stmt = mysqli_prepare($db, 'CALL multiples(?, ?)'); // Bind parameters mysqli_stmt_bind_param($stmt, 'ii', $param1, $param2); // Execute the statement mysqli_stmt_execute($stmt); // Fetch the first result set $result1 = mysqli_stmt_get_result($stmt); while ($row1 = $result1->fetch_assoc()) { echo "Result 1: " . $row1['id'] . "\n"; } // Advance to the second result set mysqli_stmt_next_result($stmt); $result2 = mysqli_stmt_get_result($stmt); // Fetch and print the second result set while ($row2 = $result2->fetch_assoc()) { echo "Result 2: " . $row2['id'] . "\n"; } // Close the statement mysqli_stmt_close($stmt);</code>
PHP 对象-定向样式:
<code class="php">// Create a prepared statement object $stmt = $db->prepare('CALL multiples(?, ?)'); // Bind parameters $stmt->bind_param('ii', $param1, $param2); // Execute the statement $stmt->execute(); // Fetch the first result set $result1 = $stmt->get_result(); while ($row1 = $result1->fetch_assoc()) { echo "Result 1: " . $row1['id'] . "\n"; } // Advance to the second result set $stmt->next_result(); // Store and print results from the second result set $result2 = $stmt->get_result(); while ($row2 = $result2->fetch_assoc()) { echo "Result 2: " . $row2['id'] . "\n"; } // Close the statement $stmt->close();</code>
附加说明:
以上是如何在 PHP 中使用 MySQLi 从存储过程中检索多个结果集?的详细内容。更多信息请关注PHP中文网其他相关文章!