PHP : How to execute a MySQLi query after a multi_query (for PHP > 5.3) ?

I just got a problem today : I needed to execute a $mysqli->query() after a $mysqli->multi_query() and it didn’t work. Here is the error I got :

Fatal error: Call to a member function fetch_array() on a non-object in [...]

I then checked the PHP Documentation about mysqli::multi_query and tried to use the solution suggested by jcn50 but I got this error :

Strict Standards: mysqli::next_result(): There is no next result set. Please, call mysqli_more_results()/mysqli::more_results() to check whether to call this function/method in [...]

Because it only works for PHP <= 5.2.

I then find a solution that is logic :

$mysqli->multi_query("Many MySQL queries concatenated by a semicolon");

while ($mysqli->next_result()) // flush multi_queries
{
  if (!$mysqli->more_results()) break;
}

$mysqli->query("MySQL statement #1");
$mysqli->query("MySQL statement #2");
$mysqli->query("MySQL statement #3");

3 Comments

  1. Leo Reiss   •  

    Thank you very much!!

  2. Edgar Walkowsky   •  

    The problem was bugging me for months until I put in this code and like magic it worked! Cheers!

Leave a Reply

Your email address will not be published. Required fields are marked *