-3

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!

Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39
Genken
  • 23
  • 5
  • Do not use `mysqli_multi_query` – Dharman May 02 '21 at 21:18
  • `SET @a = 'foo';` does not produce results. That is why you see 0 – Dharman May 02 '21 at 21:19
  • 1
    `mysqli_use_result` and `mysqli_store_result` does not make sense to be used together. Clearly you have no idea what `mysqli_multi_query` is for. DO NOT USE THIS FUNCTION – Dharman May 02 '21 at 21:20
  • @Dharman I’ve used both of functions to show their output. I know that mysqli_multi_query must be used with, this was a simple example. In my real task I need to send seven statements at a time, but the effect in this or that case is the same. – Genken May 02 '21 at 22:02
  • @Dharman so every statement makes its own output, even if it has no returned values in fact (SET, INSERT…)? – Genken May 02 '21 at 22:05
  • If you need to send 7 queries then you need to call `mysqli_query` 7 times or create 7 prepared statements. `mysqli_multi_query()` is not meant to send multiple queries at a time to the server. – Dharman May 02 '21 at 22:52

1 Answers1

0

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);
Dharman
  • 30,962
  • 25
  • 85
  • 135