Skip to content

Commit eda7492

Browse files
committed
Handle errors during next_result()
1 parent c1f8dd4 commit eda7492

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

ext/mysqli/mysqli_api.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,7 +1608,11 @@ PHP_FUNCTION(mysqli_next_result) {
16081608
}
16091609
MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
16101610

1611-
RETURN_BOOL(!mysql_next_result(mysql->mysql));
1611+
if (mysql_next_result(mysql->mysql)) {
1612+
MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
1613+
RETURN_FALSE;
1614+
}
1615+
RETURN_TRUE;
16121616
}
16131617
/* }}} */
16141618

@@ -1640,7 +1644,11 @@ PHP_FUNCTION(mysqli_stmt_next_result) {
16401644
}
16411645
MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_VALID);
16421646

1643-
RETURN_BOOL(!mysql_stmt_next_result(stmt->stmt));
1647+
if (mysql_stmt_next_result(stmt->stmt)) {
1648+
MYSQLI_REPORT_STMT_ERROR(stmt->stmt);
1649+
RETURN_FALSE;
1650+
}
1651+
RETURN_TRUE;
16441652
}
16451653
/* }}} */
16461654
#endif
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
--TEST--
2+
Error in multi query
3+
--SKIPIF--
4+
<?php
5+
require_once('skipif.inc');
6+
require_once('skipifconnectfailure.inc');
7+
?>
8+
--FILE--
9+
<?php
10+
11+
require_once __DIR__ . '/connect.inc';
12+
13+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
14+
15+
$mysqli = new mysqli($host, $user, $passwd, $db, $port, $socket);
16+
17+
$mysqli->multi_query("SELECT 1; SELECT 2; Syntax Error");
18+
19+
try {
20+
do {
21+
if ($res = $mysqli->store_result()) {
22+
var_dump($res->fetch_all(MYSQLI_ASSOC));
23+
$res->free();
24+
}
25+
} while ($mysqli->more_results() && $mysqli->next_result());
26+
} catch (mysqli_sql_exception $e) {
27+
echo $e->getMessage(), "\n";
28+
}
29+
30+
$mysqli->query("DROP PROCEDURE IF EXISTS p");
31+
$mysqli->query('CREATE PROCEDURE p() READS SQL DATA BEGIN SELECT 1; SELECT foobar FROM table_that_does_not_exist; END;');
32+
33+
$stmt = $mysqli->prepare("CALL p()");
34+
$stmt->execute();
35+
36+
try {
37+
do {
38+
$stmt->bind_result($num);
39+
while ($stmt->fetch()) {
40+
echo "num = $num\n";
41+
}
42+
} while ($stmt->more_results() && $stmt->next_result());
43+
} catch (mysqli_sql_exception $e) {
44+
echo $e->getMessage(), "\n";
45+
}
46+
47+
$mysqli->query("DROP PROCEDURE IF EXISTS p");
48+
49+
?>
50+
--EXPECTF--
51+
array(1) {
52+
[0]=>
53+
array(1) {
54+
[1]=>
55+
string(1) "1"
56+
}
57+
}
58+
array(1) {
59+
[0]=>
60+
array(1) {
61+
[2]=>
62+
string(1) "2"
63+
}
64+
}
65+
You have an error in your SQL syntax; %s
66+
num = 1
67+
Table '%s.table_that_does_not_exist' doesn't exist

ext/mysqli/tests/mysqli_report.phpt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,14 @@ Warning: mysqli_rollback(): (%s/%d): Commands out of sync; you can't run this co
331331

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

334+
Warning: mysqli_next_result(): (%s/%d): Commands out of sync; you can't run this command now in %s on line %d
335+
336+
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
337+
334338
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
335339

340+
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
341+
336342
Warning: mysqli_stmt_attr_set(): (%s/%d): Not implemented in %s on line %d
337343

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

0 commit comments

Comments
 (0)