0

I'm trying to loop through a stored procedure, called via prepared statement, in PHP. Hopefully it's not impossible. Here's my PHP code:

$database = new mysqli($server, $user, $pass, $db);
$stmt = $database->prepare("CALL thisShouldReturnEightResultSets (?)");
$stmt->bind_param("i", $id);
$stmt->execute();

do {
    if ($res = $stmt->bind_result($myvar)) {
        $stmt->fetch();
        echo "*" . $myvar . "<br/>";
    } else {
        if ($stmt->errno) {
            echo "Store failed: (" . $stmt->errno . ") " . $stmt->error;
        }
    }
} while ($stmt->next_result());

That should print like so:

* 4
* 16
* 7
etc...

... and then exit once it runs of out result sets. Instead I get:

* 4

Fatal error: Call to undefined method mysqli_stmt::next_result()

At first I tried get_result instead of bind_result, but that errored. Based on this I found that I don't have the mysqlnd driver installed. Iworked around that with bind_result. Now I need a work around for next_result which I'm guessing is part of the mysqlnd driver as well. What are my options here? Tech notes:

PHP Version 5.3.2-1ubuntu4.11

Thanks.

B.T.
  • 41
  • 7

2 Answers2

0

bindResult does not like multiple results.

Use get_result() then fetch_assoc and store in an array, Then use a foreach loop to output your results.

  $res = $stmt->get_result();
  $array[];

  while($x = $res->fetch_assoc()){
     $array[]=$x;

    }
0

I don't think you can do this with a prepared statement, since you don't have the MYSQLND driver. You'll have to do it the old fashioned way, with escaping.

$id = $database->real_escape_string($id);
if ($database->multi_query("CALL thisShouldReturnEightResultSets ('$id')")) {
    do {
        if ($result = $database->store_result()) {
            while ($row = $result->fetch_row()) {
                echo "*" . $row[0] . "<br/>";
            }
        }
    } while ($database->next_result());
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Doesn't work. Stored procedure being called has multiple selects in it. The code above would only print the '4' from the first select's result set. – B.T. Sep 22 '17 at 01:51
  • I've changed the answer to show how to do it with `multi_query`. – Barmar Sep 22 '17 at 01:55