Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <body>
- <?php
- $mysqli = new mysqli("localhost", "test", "testpass", "testdb");
- // Set up simple table with data.
- $mysqli->query("drop table if exists testtable");
- $mysqli->query("create table testtable(fname varchar(20), lname varchar(20), age int)");
- $mysqli->query("insert into testtable (fname, lname, age) values ('Bob', 'Jones', 12), ('Mary', 'Poppins', '20')");
- // Set up a stored procedure that returns two result sets.
- if (!$mysqli->query("DROP PROCEDURE IF EXISTS p") ||
- !$mysqli->query("CREATE PROCEDURE p() BEGIN select fname, lname from testtable; select fname, lname, age from testtable; END;")) {
- echo "Stored procedure creation failed: (" . $mysqli->errno . ") " . $mysqli->error;
- }
- // Prepare the statement and execute.
- if(!($stmt = $mysqli->prepare("call p()"))) {
- echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
- }
- $stmt->execute();
- // Bind result variables for the first result set
- $stmt->bind_result($fname, $lname);
- while ($stmt->fetch()) {
- echo "<p>{$fname} {$lname}</p>";
- }
- //Fetch the next result set
- $mysqli->next_result();
- //Bind the result variables for the new result set
- $stmt->bind_result($fname, $lname, $age);
- while ($stmt->fetch()) {
- echo "<p>{$fname} {$lname} is {$age} years old.";
- }
- //clean up
- $stmt->close();
- $mysqli->close();
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement