Skip to content

Commit

Permalink
Handle errors during next_result()
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Nov 11, 2020
1 parent c1f8dd4 commit eda7492
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
12 changes: 10 additions & 2 deletions ext/mysqli/mysqli_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -1608,7 +1608,11 @@ PHP_FUNCTION(mysqli_next_result) {
}
MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);

RETURN_BOOL(!mysql_next_result(mysql->mysql));
if (mysql_next_result(mysql->mysql)) {
MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */

Expand Down Expand Up @@ -1640,7 +1644,11 @@ PHP_FUNCTION(mysqli_stmt_next_result) {
}
MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_VALID);

RETURN_BOOL(!mysql_stmt_next_result(stmt->stmt));
if (mysql_stmt_next_result(stmt->stmt)) {
MYSQLI_REPORT_STMT_ERROR(stmt->stmt);
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
#endif
Expand Down
67 changes: 67 additions & 0 deletions ext/mysqli/tests/mysqli_next_result_error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
--TEST--
Error in multi query
--SKIPIF--
<?php
require_once('skipif.inc');
require_once('skipifconnectfailure.inc');
?>
--FILE--
<?php

require_once __DIR__ . '/connect.inc';

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

$mysqli = new mysqli($host, $user, $passwd, $db, $port, $socket);

$mysqli->multi_query("SELECT 1; SELECT 2; Syntax Error");

try {
do {
if ($res = $mysqli->store_result()) {
var_dump($res->fetch_all(MYSQLI_ASSOC));
$res->free();
}
} while ($mysqli->more_results() && $mysqli->next_result());
} catch (mysqli_sql_exception $e) {
echo $e->getMessage(), "\n";
}

$mysqli->query("DROP PROCEDURE IF EXISTS p");
$mysqli->query('CREATE PROCEDURE p() READS SQL DATA BEGIN SELECT 1; SELECT foobar FROM table_that_does_not_exist; END;');

$stmt = $mysqli->prepare("CALL p()");
$stmt->execute();

try {
do {
$stmt->bind_result($num);
while ($stmt->fetch()) {
echo "num = $num\n";
}
} while ($stmt->more_results() && $stmt->next_result());
} catch (mysqli_sql_exception $e) {
echo $e->getMessage(), "\n";
}

$mysqli->query("DROP PROCEDURE IF EXISTS p");

?>
--EXPECTF--
array(1) {
[0]=>
array(1) {
[1]=>
string(1) "1"
}
}
array(1) {
[0]=>
array(1) {
[2]=>
string(1) "2"
}
}
You have an error in your SQL syntax; %s
num = 1
Table '%s.table_that_does_not_exist' doesn't exist
6 changes: 6 additions & 0 deletions ext/mysqli/tests/mysqli_report.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,14 @@ Warning: mysqli_rollback(): (%s/%d): Commands out of sync; you can't run this co

Warning: mysqli_stmt_prepare(): (%s/%d): Commands out of sync; you can't run this command now in %s on line %d

Warning: mysqli_next_result(): (%s/%d): Commands out of sync; you can't run this command now in %s on line %d

Warning: mysqli_next_result(): (%s/%d): You have an error in your SQL syntax; check the manual that corresponds to your %s server version for the right syntax to use near 'FOO' at line 1 in %s on line %d

Warning: mysqli_store_result(): (%s/%d): You have an error in your SQL syntax; check the manual that corresponds to your %s server version for the right syntax to use near 'FOO' at line 1 in %s on line %d

Warning: mysqli_next_result(): (%s/%d): You have an error in your SQL syntax; check the manual that corresponds to your %s server version for the right syntax to use near 'FOO' at line 1 in %s on line %d

Warning: mysqli_stmt_attr_set(): (%s/%d): Not implemented in %s on line %d

Warning: mysqli_kill(): processid should have positive value in %s on line %d
Expand Down

0 comments on commit eda7492

Please sign in to comment.