0

I need to understand how the MySQLi multi_query() works.

I'm working on table 'stefano' with (unique) field 'f1'.

If I run

<?
$db->multi_query(
    "insert into playground.stefano (f1) values ('a'); insert into playground.stefano (f1) values ('b');"
);
$db->multi_query(
    "insert into playground.stefano (f1) values ('c'); insert into playground.stefano (f1) values ('d');"
);
?>

Only 'a' and 'b' values are filled into DB.

Please dot't tell me that I can use an unique multi_query statement with 4 different queries. I know that I can, but I need to understand why the 2nd multi_query is ignored.

Stefano Radaelli
  • 1,088
  • 2
  • 15
  • 35
  • 1
    We would tell you that you should use *not a single* multi_query statement. – Your Common Sense Dec 21 '20 at 14:35
  • @YourCommonSense I cannot as I need to fill the database with a first dataset (of about 200Mb) then do some computation and then to fill it again with a second, third, .... dataset – Stefano Radaelli Dec 21 '20 at 14:41
  • 3
    I changed the dupe link. Your second query is not ignored but just causes an error, for the obvious reasons. Either way, I would suggest to avoid multi_query in favor of multi insert or - preferred - a prepared statement in a transaction – Your Common Sense Dec 21 '20 at 16:46
  • You should never use `multi_query()`. This function is not meant for execution of multiple queries. – Dharman Dec 21 '20 at 22:42
  • I agree with these two other highly experienced developers -- use a prepared statement in a loop. – mickmackusa Dec 23 '20 at 09:54

0 Answers0