Edit report at https://bugs.php.net/bug.php?id=42923&edit=1

 ID:                 42923
 Comment by:         kishorekumar dot doreshetty at gmail dot com
 Reported by:        memso at memso dot net
 Summary:            mssql_pconnect with the same login and new_link set
                     to true causes problems
 Status:             Open
 Type:               Bug
 Package:            MSSQL related
 Operating System:   Windows 2003 Server
 PHP Version:        5.2.4
 Block user comment: N
 Private report:     N

 New Comment:

After looking into code provided. Solution : your trying to connect to other 
database without close connection to first data. if you would like work with 
multiple databases. you should release free ( close ) first connection try to 
connect next one. Line mssql_close($conn1); should be before $conn2 = 
mssql_pconnect("localhost", "sa", "******", TRUE); not at the end of code.

Modified code.

<?php
        $testquery = "SELECT EMAILADDRESS FROM adminmain WHERE ID = 1";
        
        $conn1 = mssql_pconnect("localhost", "sa", "******", TRUE);
        mssql_select_db("generic", $conn1);
        
        // First Query!
        $result = mssql_query($testquery, $conn1);
        $qarray = mssql_fetch_array($result);
        echo("First Result: " . $qarray["EMAILADDRESS"] . "<br />");
        
mssql_close($conn1);
        $conn2 = mssql_pconnect("localhost", "sa", "******", TRUE);
        mssql_select_db("buymanitou", $conn2);
        
        // Second Query!
        // NOTE THAT THIS IS ON CONN1 AGAIN!
        $result = mssql_query($testquery, $conn1);
        $qarray = mssql_fetch_array($result);
        echo("Second Result: " . $qarray["EMAILADDRESS"] . "<br />");
mssql_close($conn2);    
        
?>


Previous Comments:
------------------------------------------------------------------------
[2008-03-21 15:55:59] francisco dot caserio at gmail dot com

I'm experiencing the same problem on W2k3 on PHP 5.2.4 and 5.2.5
Apparently replacing mssql_pconnect with mssql_connect solves the problem

------------------------------------------------------------------------
[2007-10-11 03:23:14] memso at memso dot net

In the sample code provided above, I did not output the mssql_get_last_message 
if the second query failed. After changing it so that it does, I get this error:
Changed database context to 'buymanitou'.

By looking at previous bug reports, this would seem to mean that the first link 
to MSSQL had taken on the new database context of the second link, as if it 
wasn't properly creating a new link.

------------------------------------------------------------------------
[2007-10-11 02:41:02] memso at memso dot net

Description:
------------
After creating a persistent MSSQL connection, any subsequent persistent 
connections to MSSQL, using the same server and login information with the 
fourth parameter (new_link) set to TRUE, will cause problems when using the 
first link again.

This has been tested and found to occur in PHP 5.2.3 and 5.2.4 for
Windows (Windows 2003 Server) on three very different servers. A completely 
different problem occurs in PHP 5.2.3 for Linux. Since PHP 5.2.4 didn't fix the 
problem in Windows, I haven't tried it yet in Linux.

**NOTE: Using mssql_connect does NOT cause this problem. Also, if the login 
information is different for the two connections, this test example works 
properly. It seems to only occur from using mssql_pconnect with the same login 
information and $new_link set to TRUE.

Reproduce code:
---------------
<?php
        $testquery = "SELECT EMAILADDRESS FROM adminmain WHERE ID = 1";
        
        $conn1 = mssql_pconnect("localhost", "sa", "******", TRUE);
        mssql_select_db("generic", $conn1);
        
        // First Query!
        $result = mssql_query($testquery, $conn1);
        $qarray = mssql_fetch_array($result);
        echo("First Result: " . $qarray["EMAILADDRESS"] . "<br />");
        
        $conn2 = mssql_pconnect("localhost", "sa", "******", TRUE);
        mssql_select_db("buymanitou", $conn2);
        
        // Second Query!
        // NOTE THAT THIS IS ON CONN1 AGAIN!
        $result = mssql_query($testquery, $conn1);
        $qarray = mssql_fetch_array($result);
        echo("Second Result: " . $qarray["EMAILADDRESS"] . "<br />");
        
        mssql_close($conn1);
        mssql_close($conn2);
?>

Expected result:
----------------
In my test case, I am grabbing an email address from the first
administrator in the system from the first database connection. Since in
my case, the first DB connection returns fakeemailaddr...@something.net
, I expect the following output (note that changing the two mssql_pconnect to 
mssql_connect, it works as expected):

First Result: fakeemailaddr...@something.net
Second Result: fakeemailaddr...@something.net

Actual result:
--------------
On the Windows Server, this is the output:
First Result: fakeemailaddr...@something.net

Warning: mssql_query() [function.mssql-query]: Unable to set query in 
C:\...\test-mssql_pconnect.php on line 17

Warning: mssql_fetch_array(): supplied argument is not a valid MS SQL-result 
resource in C:\...\test-mssql_pconnect.php on line 18
Second Result: 


On the Linux server, PHP seems to crash or puke out. I do not get a log entry 
in my phperrors.log nor in my apache2/logs/domainname-error_log. However, as 
long as I use exit; before the second mssql_query call, I get output. If I move 
it to just afterwards, nothing shows up, so I'm assuming it's using mssql_query 
after creating the second connection that is having issues.

Since these both may be due to the same issue, I am tracking it under a single 
report.


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=42923&edit=1

Reply via email to