Doc->Bug #68383 [Opn->Csd]: more_results() && next_result() doesn't work right
| From: | nikic@php.net | Date: | Thu, 26 Nov 2020 11:58:11 +0000 |
| Subject: | Doc->Bug #68383 [Opn->Csd]: more_results() && next_result() doesn't work right | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-230636@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=68383&edit=1
ID: 68383
Updated by: nikic@php.net
Reported by: anatoliy at ukhvanovy dot name
Summary: more_results() && next_result() doesn't work right
-Status: Open
+Status: Closed
-Type: Documentation Problem
+Type: Bug
Package: MySQLi related
Operating System: windows 7
PHP Version: Irrelevant
-Assigned To:
+Assigned To: nikic
Block user comment: N
Private report: N
New Comment:
This should be fixed by https://github.com/php/php-src/commit/eda749260448c2cfbc628592c0943263d03d7119.
The lack of error reporting was a bug in PHP, not just a doc problem.
Previous Comments:
------------------------------------------------------------------------
[2014-11-09 17:49:55] anatoliy at ukhvanovy dot name
Description:
------------
---
From manual page: http://www.php.net/mysqli.quickstart.multiple-statement
---
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.
--------------------------------------------------------
(watch «test script»)
--------------------------------------------------------
Try the following code instead of while-loop. In this code (below) 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));
}
--------------------------------------------------------
Test script:
---------------
<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());
Expected 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.
Actual result:
--------------
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.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=68383&edit=1