phpmysqlmysqlimysqli-multi-query

PHP `mysqli_multi_query` cannot return a value


A simple example

$connection = mysqli_connect("domain", "user", "psw", "db");
$response = mysqli_multi_query($connection, "
    SET @a = 'foo';
    SET @b = 'bar';
    SELECT @a AS A, @b AS B
");

var_dump(mysqli_field_count(connection));
var_dump(mysqli_use_result($connection));
var_dump(mysqli_store_result($connection));
var_dump(mysqli_more_results($connection));
var_dump(mysqli_next_result($connection));

Returns (prettyfied)

int(0)
bool(false)
bool(false)
bool(true)
bool(true)

What's wrong? Am I just misunderstanding how to get values from mysqli_multi_query? Why does it return 0 in mysqli_field_count? If there is a syntax error, how can I get its number or description?

Thanks in advance!


Solution

  • Don't use mysqli_multi_query(). Each query should be sent separately to the server.

    Do it like this:

    $response1 = mysqli_query($connection, "SET @a = 'foo'");
    $response2 = mysqli_query($connection, "SET @b = 'bar'");
    $response3 = mysqli_query($connection, "SELECT @a AS A, @b AS B");
    $row = mysqli_fetch_assoc($response3);