note 116099 added to mysqli.quickstart.multiple-statement
| From: | anatoliy at ukhvanovy dot name | Date: | Sun, 09 Nov 2014 18:03:45 +0000 |
| Subject: | note 116099 added to mysqli.quickstart.multiple-statement | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-200857@lists.php.net to get a copy of this message | ||
There is an example in this documentation page:
--------------------------------------------------------
$sql = "SELECT COUNT(*) AS _num FROM test; ";
$sql.= "INSERT INTO test(id) VALUES (1); ";
$sql.= "SELECT COUNT(*) AS _num FROM test; ";
...
do {
if ($res = $mysqli->store_result()) {
var_dump($res->fetch_all(MYSQLI_ASSOC));
$res->free();
}
} while ($mysqli->more_results() && $mysqli->next_result());
--------------------------------------------------------
This is wrong example! If an error occurs while executing this query - then you will never know
about it! Because, if error occurs in some SQL-statement, more_results() will return true, but
next_result() will return false, and therefore the whole while-expression
«$mysqli->more_results() && $mysqli->next_result()» will evaluate to false and
the loop will simply stop as if there isn't any error!
Look at example below.
--------------------------------------------------------
<pre>
<?
$driver = new mysqli_driver();
$driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT;
$mysqli = new mysqli("localhost", "root", null, "my_db");
$sql = <<<'EOF'
SELECT COUNT(*) AS _num FROM my_tbl;
SYNTAX ERROR! INSERT INTO my_tbl() VALUES ();
SELECT COUNT(*) AS _num FROM my_tbl;
EOF;
if (!$mysqli->multi_query($sql)) {
echo "Multi query failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
do {
if ($res = $mysqli->store_result()) {
var_dump($res->fetch_all(MYSQLI_ASSOC));
$res->free();
}
} while ($mysqli->more_results() && $mysqli->next_result());
--------------------------------------------------------
This code should warn me about an error in line "SYNTAX ERROR! INSERT INTO my_tbl() VALUES
();". I've set report mode to MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT, so that an
exception should be thrown. But this doesn't happen! This code just gets next_result == false
and doesn't even try to execute store_result()! And if this code would try to do so, an
exception would be thrown, but this doesn't happen.
Try the following code instead of while-loop. This code throws exception, as it should be.
--------------------------------------------------------
for ($iterate = true; $iterate; ($iterate = $mysqli->more_results()) &&
$mysqli->next_result()) {
$res = $mysqli->store_result();
if($res === false) continue;
print_r($res->fetch_all(MYSQL_ASSOC));
}
--------------------------------------------------------
----
Server IP: 72.52.91.14
Probable Submitter: 109.110.94.167
----
Manual Page -- http://php.net/manual/en/mysqli.quickstart.multiple-statement.php
Edit -- https://master.php.net/note/edit/116099
Del: integrated -- https://master.php.net/note/delete/116099/integrated
Del: useless -- https://master.php.net/note/delete/116099/useless
Del: bad code -- https://master.php.net/note/delete/116099/bad+code
Del: spam -- https://master.php.net/note/delete/116099/spam
Del: non-english -- https://master.php.net/note/delete/116099/non-english
Del: in docs -- https://master.php.net/note/delete/116099/in+docs
Del: other reasons-- https://master.php.net/note/delete/116099
Reject -- https://master.php.net/note/reject/116099
Search -- https://master.php.net/manage/user-notes.php