php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #33877 When multiple result sets are not freed subsequent queries fail
Submitted: 2005-07-27 00:53 UTC Modified: 2007-08-20 13:45 UTC
Votes:9
Avg. Score:4.7 ± 0.7
Reproduced:7 of 7 (100.0%)
Same Version:6 (85.7%)
Same OS:6 (85.7%)
From: Jeffrey dot Rodriguez at gmail dot com Assigned: fmk (profile)
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: Irrelevant
Private report: No CVE-ID: None
 [2005-07-27 00:53 UTC] Jeffrey dot Rodriguez at gmail dot com
Description:
------------
NOTE:
This issue seems to occur in versions (atleast) 4.3.4 - 5.0.4

WORKAROUND:
Be sure to call mssql_free_result() on every result that has the potential to return multiple result sets.

PROBLEM:
When a query (stored procedure for example) returns multiple result sets, you have to call mssql_next_result() OR mssql_free_result() on the result of an mssql_query().

Failure to do so will cause subsequent mssql_next_query() or mssql_select_db() calls to fail.

ADDITIONAL NOTES:
The docs should be updated if this functionality is intended.

Sorry about the 'excessive' length of code, I figure you can handle 8 extra lines.

Reproduce code:
---------------
<?php
    /* -- Stored procedure
    CREATE PROCEDURE bug_proofOfConcept_sp
    AS
        SELECT 'Result set one' AS 'Result Set';
        SELECT 'Result set two' AS 'Result Set';
    GO
    */
    $link = mssql_connect("server", "user", "pass");
    mssql_select_db("database", $link);
    
    $rs = mssql_query("EXECUTE bug_proofOfConcept_sp");
    /* Note, it doesn't matter if you fetch from $rs */
    
    /* This is where things bomb out */
    if (!mssql_select_db("database", $link)) {
        echo "Broken, as expected.\n";
    }
    
    /* If we free the result things work fine again.
       Alternatively, you could call mssql_next_result($rs) */
    mssql_free_result($rs);
    
    // Select the database (3rd, and last time)
    if (!mssql_select_db("database", $link)) {
        echo "Everything should be working here; wtf?\n";
    }
?>

Expected result:
----------------
No output

Actual result:
--------------
Warning: mssql_select_db(): Unable to select database:  database in H:\proofOfConcept.php on line 16
Broken, as expected.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-07-27 01:16 UTC] Jeffrey dot Rodriguez at gmail dot com
Typo: Failure to do so will cause subsequent mssql_next_query() or mssql_select_db() calls to fail.

Should read:
Failure to do so will cause subsequent mssql_query() or mssql_select_db() calls to fail.
 [2005-07-27 19:20 UTC] fmk@php.net
SQL Server does not allow new queries when results are pending. The client must fetch all results or free unwanted results before new queries can be executed.
 [2005-07-28 00:07 UTC] Jeffrey dot Rodriguez at gmail dot com
PHP documentation does not reflect that this is the case. Since it is PHP or the libraries that PHP uses to interface with MSSQL that are bombing out, the docs should mention this. 

A meaningful warning should also be thrown.

Potentially freeing the result automatically in the background before issuing a new query since it will fail otherwise anyway.
 [2006-12-04 18:07 UTC] mike at we11er dot co dot uk
This happens with PDO as well - but on windows the nextRowset method isn't implemented which means it's /impossible/ to use stored procedures without breaking subsequent queries. closeCursor doesn't work when there are multiple rowsets.

I've submitted bug reports but it is always labelled as Won't Fix and i'm told to compile pdo_mysql myself. WTF?
 [2007-08-20 13:45 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

"If the query returns multiple results then it is necessary to fetch all results by mssql_next_result() or free the results by mssql_free_result() before executing next query."
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 00:01:41 2024 UTC