Advertisement
Guest User

Untitled

a guest
Apr 6th, 2012
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.33 KB | None | 0 0
  1. <html>
  2. <body>
  3. <?php
  4. $mysqli = new mysqli("localhost", "test", "testpass", "testdb");
  5.  
  6. // Set up simple table with data.
  7. $mysqli->query("drop table if exists testtable");
  8. $mysqli->query("create table testtable(fname varchar(20), lname varchar(20), age int)");
  9. $mysqli->query("insert into testtable (fname, lname, age) values ('Bob', 'Jones', 12), ('Mary', 'Poppins', '20')");
  10. // Set up a stored procedure that returns two result sets.
  11. if (!$mysqli->query("DROP PROCEDURE IF EXISTS p") ||
  12.         !$mysqli->query("CREATE PROCEDURE p() BEGIN select fname, lname from testtable; select fname, lname, age from testtable; END;")) {
  13.     echo "Stored procedure creation failed: (" . $mysqli->errno . ") " . $mysqli->error;
  14. }
  15.  
  16. // Prepare the statement and execute.
  17. if(!($stmt = $mysqli->prepare("call p()"))) {
  18.     echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
  19. }
  20. $stmt->execute();
  21. // Bind result variables for the first result set
  22. $stmt->bind_result($fname, $lname);
  23.  
  24. while ($stmt->fetch()) {
  25.     echo "<p>{$fname} {$lname}</p>";
  26. }
  27.  
  28. //Fetch the next result set
  29. $mysqli->next_result();
  30. //Bind the result variables for the new result set
  31. $stmt->bind_result($fname, $lname, $age);
  32.  
  33. while ($stmt->fetch()) {
  34.     echo "<p>{$fname} {$lname} is {$age} years old.";
  35. }
  36.  
  37. //clean up
  38. $stmt->close();
  39. $mysqli->close();
  40. ?>
  41. </body>
  42. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement