Round Posted October 2, 2013 Share Posted October 2, 2013 (edited) Hello All, I hope someone can help. Recently and randomly mssql_connect is failing. I am using the code below to make the db connection: $conn = @mssql_connect( "SERVER", "username", "password" ) or die( "Err:conn"); $rs = @mssql_select_db( "database", $conn) or die( "ERR:Db"); For some reason every now and then a page will report Err:conn and a few page refreshes will then connect. Am I hitting a maximum connection number? Can anyone shed any light? Any help much appreciated. Build: PHP Version 5.2.6 Windows 2003 R2 Microsoft-IIS/6.0 Thanks Edited October 2, 2013 by Round Quote Link to comment https://forums.phpfreaks.com/topic/282638-mssql_connect-failing/ Share on other sites More sharing options...
Ch0cu3r Posted October 2, 2013 Share Posted October 2, 2013 (edited) Remove the error suppression operator (the @ sign) from infront of any mssql_*() function calls. Set error error_reporting to E_ALL and display_errors to On within the php.ini. Add mssql_get_last_message() to or die() $conn = mssql_connect( "SERVER", "username", "password" ) or die( "Err:conn - " . mssql_get_last_message()); $rs = mssql_select_db( "database", $conn) or die( "ERR:Db- " . mssql_get_last_message()); Post all error messages in full here. Edited October 2, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/282638-mssql_connect-failing/#findComment-1452196 Share on other sites More sharing options...
Round Posted October 2, 2013 Author Share Posted October 2, 2013 Hello Ch0cu3r, Thanks for the reply. I now get: Err:conn - Changed database context to 'databasename'. Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/282638-mssql_connect-failing/#findComment-1452236 Share on other sites More sharing options...
Ch0cu3r Posted October 2, 2013 Share Posted October 2, 2013 I do not use mssql databases so I cant really help. It appears it is suggested to do the following $conn = mssql_connect( "SERVER", "username", "password" ); if(!$conn) die( "Err:conn - " . mssql_get_last_message()); $rs = mssql_select_db( "database", $conn); if(!rs) die( "ERR:Db- " . mssql_get_last_message()); as the api php uses for mssql returns status messages and these are what is triggering the or die, even though there isn't an error. So best to check if mssql_connect, mssql_select_db returned false. Quote Link to comment https://forums.phpfreaks.com/topic/282638-mssql_connect-failing/#findComment-1452248 Share on other sites More sharing options...
DavidAM Posted October 2, 2013 Share Posted October 2, 2013 $conn = mssql_connect( "SERVER", "username", "password" ); if(!$conn) die( "Err:conn - " . mssql_get_last_message()); Is equivalent to $conn = mssql_connect( "SERVER", "username", "password" ) or die( "Err:conn - " . mssql_get_last_message());It is essentially the same code. On the other hand, "Err:conn - Changed database context to 'databasename'." does not look like an ERROR message to me (except that it comes from your die() statement. Is it possible you copy-pasted that or die() and forgot to change the prefix ("Err:conn")? What I am saying is that I would think that is an informational message, which should not cause the connection to fail, so $conn should have a value and the or die() should not happen. I've never used mssql, but a quick search though the PHP manual gives me the impression you may want to look at mssql_min_message_severity and/or mssql_min_error_severity or the associated php.ini settings. Quote Link to comment https://forums.phpfreaks.com/topic/282638-mssql_connect-failing/#findComment-1452324 Share on other sites More sharing options...
Round Posted October 8, 2013 Author Share Posted October 8, 2013 Hello, I definitely didn't cut and paste error. I added the get mssql_get_last_message to be connection include file and then loaded a page. After a few refreshes as it's intermittent I recieved the error and copied it from the page. Obviously I changed the dbname to a generic 1 as to not show my actual. I'll look at the ini settings you suggest. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/282638-mssql_connect-failing/#findComment-1453040 Share on other sites More sharing options...
DavidAM Posted October 8, 2013 Share Posted October 8, 2013 ... it's intermittent ... That's probably ... Oops, I guess I missed it in the initial post: ... Recently and randomly mssql_connect is failing. Random connection errors could be caused by overloading the database server. Make sure you only connect to the database ONE TIME per page request (script). Do not OPEN and CLOSE and OPEN and CLOSE .... the connection. It takes a little while for the connection to completely go away, so you could be reaching the connection limit for the server. However, I don't understand why the printed message "Changed database context to 'databasename'", would be intermittent. I would expect that would happen every time; unless ... Are you using persistent connections? I hope someone with actual MSSQL experience comes along to help more. Quote Link to comment https://forums.phpfreaks.com/topic/282638-mssql_connect-failing/#findComment-1453141 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.