php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42923 mssql_pconnect with the same login and new_link set to true causes problems
Submitted: 2007-10-11 02:41 UTC Modified: 2016-10-15 23:12 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: memso at memso dot net Assigned:
Status: Wont fix Package: MSSQL related
PHP Version: 5.2.4 OS: Windows 2003 Server
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
36 - 14 = ?
Subscribe to this entry?

 
 [2007-10-11 02:41 UTC] 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 fakeemailaddress@something.net
, I expect the following output (note that changing the two mssql_pconnect to mssql_connect, it works as expected):

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

Actual result:
--------------
On the Windows Server, this is the output:
First Result: fakeemailaddress@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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-10-11 03:23 UTC] 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.
 [2008-03-21 15:55 UTC] 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
 [2012-11-20 11:37 UTC] kishorekumar dot doreshetty at gmail dot com
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);	
	
?>
 [2016-10-15 23:12 UTC] kalle@php.net
-Status: Open +Status: Wont fix
 [2016-10-15 23:12 UTC] kalle@php.net
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-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 20:01:32 2024 UTC