note 106126 modified in mysqli.multi-query by danbrown

From: Date: Thu, 13 Oct 2011 17:28:26 +0000
Subject: note 106126 modified in mysqli.multi-query by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-183855@lists.php.net to get a copy of this message
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: <?php $statements = array("INSERT INTO tablename VALUES ('1', 'one')", "INSERT INTO tablename VALUES ('2', 'two')"); if ($mysqli->multi_query(implode(';', $statements))) { $i = 0; do { $i++; } while ($mysqli->next_result()); } if ($mysqli->errno) { echo "Batch execution prematurely ended on statement $i.\n"; var_dump($statements[$i], $mysqli->error); } ?> The IF statement on the multi_query() call checks the first result, because next_result() starts at the second. --was-- 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: $statements = array("INSERT INTO tablename VALUES ('1', 'one')", "INSERT INTO tablename VALUES ('2', 'two')"); if ($mysqli->multi_query(implode(';', $statements))) { $i = 0; do { $i++; } while ($mysqli->next_result()); } if ($mysqli->errno) { echo "Batch execution prematurely ended on statement $i.\n"; var_dump($statements[$i], $mysqli->error); } The IF statement on the multi_query() call checks the first result, because next_result() starts at the second. http://php.net/manual/en/mysqli.multi-query.php

« previous php.notes (#183855) next »