I was developing my own CMS and I was having problem with attaching the database' sql file. I thought mysqli_multi_query got bugs where it crashes my MySQL server. I tried to report the bug but it showed that it has duplicate bug reports of other developers. To my surprise, mysqli_multi_query needs to bother with result even if there's none.

I finally got it working when I copied the sample and removed somethings. Here is what it looked liked

Here are more details about error checking and return values from multi_query(). Testing shows that there are some mysqli properties to check for each result:

affected_rows
errno
error
insert_id
warning_count

If error or errno are not empty then the remaining queries did not return anything, even though error and errno will appear to be empty if processing further results is continued.

Also note that get_warnings() will not work with multi_query(). It can only be used after looping through all results, and it will only get the warnings for the last one of the queries and not for any others. If you need to see or log query warning strings then you must not use multi_query(), because you can only see the warning_count value.

If you experience "lost connection to MySQL server" errors with your Stored Procedure calls then you did not fetch the 'OK' (or 'ERR') message, which is a second result-set from a Stored Procedure call. You have to fetch that result to have no problems with subsequent queries.

Bad example, will FAIL now and then on subsequent calls:<?php$sQuery='CALL exampleSP('param')';if(!mysqli_multi_query($this->sqlLink,$sQuery))$this->queryError();$this->sqlResult=mysqli_store_result($this->sqlLink);?>

Working example:<?php$sQuery='CALL exampleSP('param')';if(!mysqli_multi_query($this->sqlLink,$sQuery))$this->queryError();$this->sqlResult=mysqli_store_result($this->sqlLink);

Of course you can do more with the multiple results then just throwing them away, but for most this will suffice. You could for example make an "sp" function which will kill the 2nd 'ok' result.

This nasty 'OK'-message made me spend hours trying to figure out why MySQL server was logging warnings with 'bad packets from client' and PHP mysql_error() with 'Connection lost'. It's a shame the mysqli library does catch this by just doing it for you.

I'd like to reinforce the correct way of catching errors from the queries executed by multi_query(), since the manual's examples don't show it and it's easy to lose UPDATEs, INSERTs, etc. without knowing it.

$mysqli->next_result() will return false if it runs out of statements OR if the next statement has an error. Therefore, it's important to check for errors when the loop ends. Also, I believe it's useful to know when and where the loop broke, so consider the following code:

If you want to create a table with triggers, procedures or functions in one multiline query you may stuck with a error -#1064 - You have an error in your SQL syntax; xxx corresponds to your MySQL server version for the right syntax to use near 'DELIMITER' at line 1

The solution is very simple - don't use DELIMITER keyword at all! So, instead of :

DELIMITER |CREATE TRIGGER $dbName.$iname BEFORE INSERT ON $table FOR EACH ROWBEGIN <body>EOT|DELIMITER ;

just use :

CREATE TRIGGER $dbName.$iname BEFORE INSERT ON $table FOR EACH ROWBEGIN <body>EOT;

For more information read answers at StackOverflow for question #5311141

Be sure to not send a set of queries that are larger than max_allowed_packet size on your MySQL server. If you do, you'll get an error like: Mysql Error (1153): Got a packet bigger than 'max_allowed_packet' bytes

To see your MySQL size limitation, run the following query: show variables like 'max_allowed_packet';

If you're importing a sql-file with triggers, functions, stored procedures and other stuff, you'll might be using DELIMITER in MySQL.Notice: This function assumes that all delimiters are on it's own line, and that "DELIMITER" are in all caps.