php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #68383 more_results() && next_result() doesn't work right
Submitted: 2014-11-09 17:49 UTC Modified: 2017-01-28 12:48 UTC
From: anatoliy at ukhvanovy dot name Assigned:
Status: Open Package: MySQLi related
PHP Version: Irrelevant OS: windows 7
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [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
 
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Thu Nov 15 17:01:26 2018 UTC