Doc #68383 [NEW]: more_results() && next_result() doesn't work right
| From: | anatoliy at ukhvanovy dot name | Date: | Sun, 09 Nov 2014 17:49:56 +0000 |
| Subject: | Doc #68383 [NEW]: more_results() && next_result() doesn't work right | ||
| Groups: | php.doc.bugs | ||
| Request: | Send a blank email to doc-bugs+get-11623@lists.php.net to get a copy of this message | ||
From: anatoliy at ukhvanovy dot name
Operating system: windows 7
PHP version: Irrelevant
Package: Documentation problem
Bug Type: Documentation Problem
Bug description:more_results() && next_result() doesn't work right
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 bug report at https://bugs.php.net/bug.php?id=68383&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=68383&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=68383&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=68383&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=68383&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=68383&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=68383&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=68383&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=68383&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=68383&r=support
Expected behavior: https://bugs.php.net/fix.php?id=68383&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=68383&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=68383&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=68383&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=68383&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=68383&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=68383&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=68383&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=68383&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=68383&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=68383&r=mysqlcfg