From eda749260448c2cfbc628592c0943263d03d7119 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Wed, 11 Nov 2020 15:56:05 +0100 Subject: Handle errors during next_result() --- ext/mysqli/mysqli_api.c | 12 ++++- ext/mysqli/tests/mysqli_next_result_error.phpt | 67 ++++++++++++++++++++++++++ ext/mysqli/tests/mysqli_report.phpt | 6 +++ 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 ext/mysqli/tests/mysqli_next_result_error.phpt diff --git a/ext/mysqli/mysqli_api.c b/ext/mysqli/mysqli_api.c index 9a54171b30..9896ab8eda 100644 --- a/ext/mysqli/mysqli_api.c +++ b/ext/mysqli/mysqli_api.c @@ -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; } /* }}} */ @@ -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 diff --git a/ext/mysqli/tests/mysqli_next_result_error.phpt b/ext/mysqli/tests/mysqli_next_result_error.phpt new file mode 100644 index 0000000000..4118bd0eab --- /dev/null +++ b/ext/mysqli/tests/mysqli_next_result_error.phpt @@ -0,0 +1,67 @@ +--TEST-- +Error in multi query +--SKIPIF-- + +--FILE-- +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 diff --git a/ext/mysqli/tests/mysqli_report.phpt b/ext/mysqli/tests/mysqli_report.phpt index 3373097697..7a343bdef8 100644 --- a/ext/mysqli/tests/mysqli_report.phpt +++ b/ext/mysqli/tests/mysqli_report.phpt @@ -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 -- cgit v1.2.1