Skip to content

Commit 186b766

Browse files
committed
PDO MySQL: Make sure nextRowset() works with partially consumed result
This was already working in all cases apart from native prepared statements with unbuffered queries. In that case invoking stmt_free_result() addresses the issue.
1 parent 23193e8 commit 186b766

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

ext/pdo_mysql/mysql_statement.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,15 @@ static int pdo_mysql_stmt_next_rowset(pdo_stmt_t *stmt) /* {{{ */
349349
PDO_DBG_ENTER("pdo_mysql_stmt_next_rowset");
350350
PDO_DBG_INF_FMT("stmt=%p", S->stmt);
351351

352+
/* ensure that we free any previous unfetched results */
353+
if (S->stmt) {
354+
mysql_stmt_free_result(S->stmt);
355+
}
356+
if (S->result) {
357+
mysql_free_result(S->result);
358+
S->result = NULL;
359+
}
360+
352361
#ifdef PDO_USE_MYSQLND
353362
if (!H->emulate_prepare) {
354363
if (!mysqlnd_stmt_more_results(S->stmt)) {
@@ -359,11 +368,6 @@ static int pdo_mysql_stmt_next_rowset(pdo_stmt_t *stmt) /* {{{ */
359368
PDO_DBG_RETURN(0);
360369
}
361370

362-
/* TODO - this code is stolen from execute() - see above */
363-
if (S->result) {
364-
mysql_free_result(S->result);
365-
S->result = NULL;
366-
}
367371
{
368372
/* for SHOW/DESCRIBE and others the column/field count is not available before execute */
369373
int i;
@@ -394,17 +398,6 @@ static int pdo_mysql_stmt_next_rowset(pdo_stmt_t *stmt) /* {{{ */
394398
}
395399
#endif
396400

397-
/* ensure that we free any previous unfetched results */
398-
#ifndef PDO_USE_MYSQLND
399-
if (S->stmt) {
400-
mysql_stmt_free_result(S->stmt);
401-
}
402-
#endif
403-
if (S->result) {
404-
mysql_free_result(S->result);
405-
S->result = NULL;
406-
}
407-
408401
if (!mysql_more_results(H->server)) {
409402
/* No more results */
410403
PDO_DBG_RETURN(0);

ext/pdo_mysql/tests/pdo_mysql_stmt_nextrowset.phpt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ if (!MySQLPDOTest::isPDOMySQLnd())
5757
} while ($stmt->nextRowSet());
5858
var_dump($stmt->nextRowSet());
5959

60+
echo "Skip fetchAll(): ";
61+
unset($stmt);
62+
$stmt = $db->query('CALL p()');
63+
var_dump($stmt->nextRowSet());
64+
$stmt->closeCursor();
6065
}
6166

6267
try {
@@ -160,6 +165,7 @@ array(3) {
160165
array(0) {
161166
}
162167
bool(false)
168+
Skip fetchAll(): bool(true)
163169
array(1) {
164170
[0]=>
165171
array(1) {
@@ -211,6 +217,7 @@ array(3) {
211217
array(0) {
212218
}
213219
bool(false)
220+
Skip fetchAll(): bool(true)
214221
Native PS...
215222
array(1) {
216223
[0]=>
@@ -263,6 +270,7 @@ array(3) {
263270
array(0) {
264271
}
265272
bool(false)
273+
Skip fetchAll(): bool(true)
266274
array(1) {
267275
[0]=>
268276
array(1) {
@@ -314,4 +322,5 @@ array(3) {
314322
array(0) {
315323
}
316324
bool(false)
325+
Skip fetchAll(): bool(true)
317326
done!

0 commit comments

Comments
 (0)