note 106126 added to mysqli.multi-query
| From: | crmccar at gmail dot com | Date: | Thu, 13 Oct 2011 01:40:15 +0000 |
| Subject: | note 106126 added to mysqli.multi-query | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-183838@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:
$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.
----
Server IP: 69.147.83.197
Probable Submitter: 69.211.177.2
----
Manual Page -- http://www.php.net/manual/en/mysqli.multi-query.php
Edit -- https://master.php.net/note/edit/106126
Del: integrated -- https://master.php.net/note/delete/106126/integrated
Del: useless -- https://master.php.net/note/delete/106126/useless
Del: bad code -- https://master.php.net/note/delete/106126/bad+code
Del: spam -- https://master.php.net/note/delete/106126/spam
Del: non-english -- https://master.php.net/note/delete/106126/non-english
Del: in docs -- https://master.php.net/note/delete/106126/in+docs
Del: other reasons-- https://master.php.net/note/delete/106126
Reject -- https://master.php.net/note/reject/106126
Search -- https://master.php.net/manage/user-notes.php