Jump to content

Archived

This topic is now archived and is closed to further replies.

Round

mssql_connect failing

Recommended Posts

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Hello Ch0cu3r,

 

Thanks for the reply.

 

I now get:

 

Err:conn - Changed database context to 'databasename'.

 

Many thanks

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

 

$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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

... 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.

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.