Jump to content


Photo

mssql_connect failing


  • Please log in to reply
6 replies to this topic

#1 Round

Round

    Advanced Member

  • Members
  • PipPipPip
  • 99 posts
  • LocationEngland

Posted 02 October 2013 - 06:12 AM

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 by Round, 02 October 2013 - 06:12 AM.

If it all gets too much just remember that, at the end of the day... It gets dark!! or that on the other hand... There are more Fingers!!
If you eventually get it right then Hip Hip ARRAY!! lol


#2 Ch0cu3r

Ch0cu3r

    Advanced Member

  • Moderators
  • 1,709 posts

Posted 02 October 2013 - 07:29 AM

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 by Ch0cu3r, 02 October 2013 - 07:32 AM.


#3 Round

Round

    Advanced Member

  • Members
  • PipPipPip
  • 99 posts
  • LocationEngland

Posted 02 October 2013 - 09:37 AM

Hello Ch0cu3r,

 

Thanks for the reply.

 

I now get:

 

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

 

Many thanks


If it all gets too much just remember that, at the end of the day... It gets dark!! or that on the other hand... There are more Fingers!!
If you eventually get it right then Hip Hip ARRAY!! lol


#4 Ch0cu3r

Ch0cu3r

    Advanced Member

  • Moderators
  • 1,709 posts

Posted 02 October 2013 - 10:04 AM

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.



#5 DavidAM

DavidAM

    Advanced Member

  • Gurus
  • 1,949 posts
  • LocationSpring, TX USA

Posted 02 October 2013 - 03:30 PM

$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.
-- I haven't lost my mind, it's backed up on tape ... somewhere!

#6 Round

Round

    Advanced Member

  • Members
  • PipPipPip
  • 99 posts
  • LocationEngland

Posted 08 October 2013 - 06:01 AM

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


If it all gets too much just remember that, at the end of the day... It gets dark!! or that on the other hand... There are more Fingers!!
If you eventually get it right then Hip Hip ARRAY!! lol


#7 DavidAM

DavidAM

    Advanced Member

  • Gurus
  • 1,949 posts
  • LocationSpring, TX USA

Posted 08 October 2013 - 01:34 PM

... 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.
-- I haven't lost my mind, it's backed up on tape ... somewhere!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com