php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68383 more_results() && next_result() doesn't work right
Submitted: 2014-11-09 17:49 UTC Modified: 2020-11-26 11:58 UTC
From: anatoliy at ukhvanovy dot name Assigned: nikic (profile)
Status: Closed Package: MySQLi related
PHP Version: Irrelevant OS: windows 7
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: anatoliy at ukhvanovy dot name
New email:
PHP Version: OS:

 

 [2014-11-09 17:49 UTC] 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.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-01-28 12:48 UTC] cmb@php.net
-Package: Documentation problem +Package: MySQLi related
 [2020-11-26 11:58 UTC] nikic@php.net
-Status: Open +Status: Closed -Type: Documentation Problem +Type: Bug -Assigned To: +Assigned To: nikic
 [2020-11-26 11:58 UTC] nikic@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 13:01:30 2024 UTC