1

I execute query of stored procudure which have 3 result of select

select _id a;
select 2 a;
select 3 a; 

and I've mysqli like this to call the Stored Procedure

$i = 0;
if ($server->connection->query("CALL sp_select(1)", MYSQLI_USE_RESULT)) {
    printf("res\n");
    do {
        $i++;
        printf("$i.do\n");
        if ($result = $server->connection->store_result()) {
            printf("$i.store\n");
            $data = $result->fetch_all(MYSQLI_ASSOC);
            $this->data[] = $data;
            $result->free_result();
            printf("$i.free\n");
        }
        if ($server->connection->more_results()) {
            printf("$i.more\n");
        }
    } while ($server->connection->next_result());
    echo json_encode($this->data);
}

That function can't get stored first result. I only got second and third result. Why I can't get stored first result?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Youngz ie
  • 659
  • 2
  • 7
  • 17

1 Answers1

0

When calling stored procedure with mysqli_query() the first result is returned from the function itself. Something like this should also gather the first result.

$result = $mysqli->query("CALL sp_select()");
$this->data[] = $result->fetch_all();
while ($mysqli->next_result()) {
    if ($result = $mysqli->store_result()) {
        $this->data[] = $result->fetch_all();
    }
}
echo json_encode($this->data);

It looks like you mixed up query() with multi_query() which does not return the initial result. This code will do the same:

$mysqli->multi_query("CALL sp_select()");
do {
    if ($result = $mysqli->store_result()) {
        $this->data[] = $result->fetch_all();
    }
} while ($mysqli->next_result());
echo json_encode($this->data);
Dharman
  • 30,962
  • 25
  • 85
  • 135