note 116099 deleted from mysqli.quickstart.multiple-statement by salathe
| From: | salathe@php.net | Date: | Wed, 12 Nov 2014 14:40:27 +0000 |
| Subject: | note 116099 deleted from mysqli.quickstart.multiple-statement by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-200881@lists.php.net to get a copy of this message | ||
Note Submitter: anatoliy at ukhvanovy dot name
----
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));
}
--------------------------------------------------------