|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65693 mssql_fetch_batch does not fetch next batch
Submitted: 2013-09-18 05:09 UTC Modified: 2016-10-15 23:18 UTC
From: ken-phpbugs at norganna dot com Assigned:
Status: Wont fix Package: MSSQL related
PHP Version: 5.5.3 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: ken-phpbugs at norganna dot com
New email:
PHP Version: OS:


 [2013-09-18 05:09 UTC] ken-phpbugs at norganna dot com
When sending batch requests to the mssql server, the mssql result is having 
dbcancel() called prematurely on the handle (via the _free_mssql_result function).

Because we are processing batches of the result set we need the query to stay open 
until we are finished fetching all the rows (not just the first batch).

In a non-batch scenario, it is fine to cancel the query after the first batch is 
called, because all rows have been fetched.

I have attached a patch, which "fixes" the problem, but probably not in the right 
way. I have no clue what the right way actually would be or what the repercussions 
of applying the patch are. It is meant for illustrative purposes only.

Test script:
// Connect to MSSQL and select the database
$link = mssql_connect('hostpath', 'user', 'pass');
mssql_select_db('db', $link);

// Send a query to a table that has 12 rows
print "Given a table with 12 rows:\n";
$result = mssql_query('SELECT * FROM tablename', $link, 5);

do {
    while ($row = mssql_fetch_assoc($result)) {
        print "Row found\n";

    $remain = mssql_fetch_batch($res);
    print "Remainder $remain\n";
} while ($remain > 0);

Expected result:
Given a table with 12 rows:
Row found
Row found
Row found
Row found
Row found
Remainder 5
Row found
Row found
Row found
Row found
Row found
Remainder 2
Row found
Row found
Remainder 0

Actual result:
Given a table with 12 rows:
Row found
Row found
Row found
Row found
Row found
Remainder 1
Row found
Remainder 0


batch-query-fix (last revision 2013-09-18 05:10 UTC by ken-phpbugs at norganna dot com)

Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2013-09-18 05:47 UTC] ken-phpbugs at norganna dot com
I made a minor copy/paste fail with the test script:
  $remain = mssql_fetch_batch($res);

$res should be $result.

Bug is still valid.
 [2013-09-19 00:41 UTC] ken-phpbugs at norganna dot com
Actually, the patch I provided does not help if you are running multiple queries. 
It seems that the dbcancel() for any query on the link cancels all queries on the 
link, so that if another query decides to free a result, your completely separate 
batch result is nuked at the same time.

The only way I can work out to make it multi-query safe is to comment out the 
dbcancel() line altogether.
 [2016-10-15 23:18 UTC]
-Status: Open +Status: Wont fix
 [2016-10-15 23:18 UTC]
With MSSQL being removed from PHP as of PHP7.0, and ext/mssql not having a maintainer, I'm gonna close this report as a Won't fix, until maybe one day it will find a new maintainer.

Alternatively you can use sqlsrv from Microsoft if you are on Windows, or pdo_dblib if you are on Unix.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Wed Mar 03 19:01:25 2021 UTC