Skip to content

Commit ab84623

Browse files
committed
Merge branch 'PHP-7.4' into PHP-8.0
* PHP-7.4: MySQLnd: Support cursors in store/get result
2 parents 1860ef2 + bc16684 commit ab84623

File tree

5 files changed

+231
-78
lines changed

5 files changed

+231
-78
lines changed

NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ PHP NEWS
88
- MySQLi:
99
. Fixed bug #67983 (mysqlnd with MYSQLI_OPT_INT_AND_FLOAT_NATIVE fails to
1010
interpret bit columns). (Nikita)
11+
. Fixed bug #64638 (Fetching resultsets from stored procedure with cursor
12+
fails). (Nikita)
13+
. Fixed bug #72862 (segfault using prepared statements on stored procedures
14+
that use a cursor). (Nikita)
15+
. Fixed bug #77935 (Crash in mysqlnd_fetch_stmt_row_cursor when calling an SP
16+
with a cursor). (Nikita)
1117

1218
- PDO_Firebird:
1319
. Fixed bug #80521 (Parameters with underscores no longer recognized). (cmb,

ext/mysqli/tests/bug77935.phpt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
--TEST--
2+
Bug #77935: Crash in mysqlnd_fetch_stmt_row_cursor when calling an SP with a cursor
3+
--SKIPIF--
4+
<?php
5+
require_once('skipif.inc');
6+
require_once('skipifconnectfailure.inc');
7+
?>
8+
--FILE--
9+
<?php
10+
require_once(__DIR__ . '/connect.inc');
11+
12+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
13+
$db = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket);
14+
$db->query('DROP PROCEDURE IF EXISTS testSp');
15+
$db->query(<<<'SQL'
16+
CREATE
17+
PROCEDURE `testSp`()
18+
BEGIN
19+
DECLARE `cur` CURSOR FOR SELECT 1;
20+
OPEN `cur`;
21+
CLOSE `cur`;
22+
SELECT 1;
23+
END;
24+
SQL);
25+
26+
$stmt = $db->prepare("CALL testSp()");
27+
$stmt->execute();
28+
$result = $stmt->get_result();
29+
while ($row = $result->fetch_assoc()) {
30+
var_dump($row);
31+
}
32+
33+
?>
34+
--EXPECT--
35+
array(1) {
36+
[1]=>
37+
int(1)
38+
}

ext/mysqli/tests/mysqli_stmt_get_result.phpt

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ if (!function_exists('mysqli_stmt_get_result'))
109109

110110
mysqli_stmt_close($stmt);
111111

112-
// get_result cannot be used in PS cursor mode
112+
// get_result can be used in PS cursor mode
113113
if (!$stmt = mysqli_stmt_init($link))
114114
printf("[030] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
115115

@@ -122,23 +122,10 @@ if (!function_exists('mysqli_stmt_get_result'))
122122
if (!mysqli_stmt_execute($stmt))
123123
printf("[033] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
124124

125-
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
126-
try {
127-
$res = mysqli_stmt_get_result($stmt);
128-
// we expect no segfault if we try to fetch a row because get_result should throw an error or return false
129-
mysqli_fetch_assoc($res);
130-
} catch (\mysqli_sql_exception $e) {
131-
echo $e->getMessage() . "\n";
132-
}
133-
134-
try {
135-
$res = $stmt->get_result();
136-
// we expect no segfault if we try to fetch a row because get_result should throw an error or return false
137-
$res->fetch_assoc();
138-
} catch (\mysqli_sql_exception $e) {
139-
echo $e->getMessage() . "\n";
125+
$result = mysqli_stmt_get_result($stmt);
126+
while ($row = mysqli_fetch_assoc($result)) {
127+
var_dump($row);
140128
}
141-
mysqli_report(MYSQLI_REPORT_OFF);
142129

143130
if (!$stmt = mysqli_stmt_init($link))
144131
printf("[034] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
@@ -196,8 +183,18 @@ if (!function_exists('mysqli_stmt_get_result'))
196183
mysqli_stmt object is not fully initialized
197184
mysqli_stmt object is not fully initialized
198185
mysqli_stmt object is not fully initialized
199-
mysqli_stmt_get_result() cannot be used with cursors
200-
get_result() cannot be used with cursors
186+
array(2) {
187+
["id"]=>
188+
int(1)
189+
["label"]=>
190+
string(1) "a"
191+
}
192+
array(2) {
193+
["id"]=>
194+
int(2)
195+
["label"]=>
196+
string(1) "b"
197+
}
201198
[040] [2014] [Commands out of sync; you can't run this command now]
202199
[041] [0] []
203200
array(2) {
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
--TEST--
2+
PS using cursor and returning multiple result sets
3+
--SKIPIF--
4+
<?php
5+
require_once('skipif.inc');
6+
require_once('skipifconnectfailure.inc');
7+
?>
8+
--FILE--
9+
<?php
10+
require_once(__DIR__ . '/connect.inc');
11+
12+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
13+
$db = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket);
14+
$db->query('DROP PROCEDURE IF EXISTS testPs');
15+
$db->query(<<<'SQL'
16+
CREATE PROCEDURE testPs() BEGIN
17+
DECLARE testCursor CURSOR FOR SELECT 'stuff';
18+
OPEN testCursor;
19+
CLOSE testCursor;
20+
SELECT 1 as a, 2 as b;
21+
SELECT 3 as a, 4 as b;
22+
END
23+
SQL
24+
);
25+
26+
echo "use_result:\n";
27+
$stmt = $db->prepare("call testPs()");
28+
$stmt->execute();
29+
$stmt->bind_result($v1, $v2);
30+
while ($stmt->fetch()) {
31+
var_dump($v1, $v2);
32+
}
33+
34+
$stmt->next_result();
35+
$stmt->bind_result($v1, $v2);
36+
while ($stmt->fetch()) {
37+
var_dump($v1, $v2);
38+
}
39+
$stmt->next_result();
40+
41+
echo "\nstore_result:\n";
42+
$stmt = $db->prepare("call testPs()");
43+
$stmt->execute();
44+
$stmt->store_result();
45+
$stmt->bind_result($v1, $v2);
46+
while ($stmt->fetch()) {
47+
var_dump($v1, $v2);
48+
}
49+
50+
$stmt->next_result();
51+
$stmt->store_result();
52+
$stmt->bind_result($v1, $v2);
53+
while ($stmt->fetch()) {
54+
var_dump($v1, $v2);
55+
}
56+
$stmt->next_result();
57+
58+
echo "\nget_result:\n";
59+
$stmt = $db->prepare("call testPs()");
60+
$stmt->execute();
61+
$result = $stmt->get_result();
62+
while ($row = $result->fetch_assoc()) {
63+
var_dump($row);
64+
}
65+
66+
$stmt->next_result();
67+
$result = $stmt->get_result();
68+
while ($row = $result->fetch_assoc()) {
69+
var_dump($row);
70+
}
71+
$stmt->next_result();
72+
73+
?>
74+
--EXPECT--
75+
use_result:
76+
int(1)
77+
int(2)
78+
int(3)
79+
int(4)
80+
81+
store_result:
82+
int(1)
83+
int(2)
84+
int(3)
85+
int(4)
86+
87+
get_result:
88+
array(2) {
89+
["a"]=>
90+
int(1)
91+
["b"]=>
92+
int(2)
93+
}
94+
array(2) {
95+
["a"]=>
96+
int(3)
97+
["b"]=>
98+
int(4)
99+
}

0 commit comments

Comments
 (0)