views:

239

answers:

1

I am a newbie to both PHP and Wordpress (but do ok in C#), and am struggling to understand the error handling in a custom plugin I am attempting to write. The basics of the plugin is to query an exsiting MSSQL database (note its not the standard MYSQL db...) and return the rows back to the screen. This was working well, but the hosting provider has taken my database offline, which led me to the error handling problem (which I thought was ok).

The following code is failing to connect to the database (as expected), but puts an error onto the screen and stops the page processing. It does not even output the 'or die' error text.

QUESTION: How can I just output a simple "Cant load data" message, and continue on normally?

function generateData()

{

 global $post; 

 if ("$post->post_title" == "Home")
 { 
  try 
  {
   $myServer = "<servername>";
   $myUser = "<username>";
   $myPass = "<password>";
   $myDB = "<dbName>"; 

   //connection to the database
   $dbhandle = mssql_connect($myServer, $myUser, $myPass) 
                              or die("Couldn't open database $myDB"); 


            //... query processing here...
        }
        catch (Exception $e)
  {
         echo "Cannot load data<br />";
  }

 }

 return $content;
}

Error being generated: (line 31 is $dbhandle = mssql_connect...)

Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: <servername> in <file path> on line 31

Fatal error: Maximum execution time of 30 seconds exceeded in <file path> on line 31

+4  A: 

First of all, if mssql_connect raises a warning when there's a problem, there is not much you can do to avoid it : the only thing you could do is hide it, using the @ operator :

if (($dbhandle = @mssql_connect($myServer, $myUser, $myPass)) === false) {
    // connection failed
}

Note : you should not die() when a connection error occurs : it'll stop the execution of the whole application, which is most certainly not desired.


The Fatal Error is a second problem (which is probably a consequence of the first one).

Note that you cannot recover from a Fatal Error : it is Fatal. Which means you must avoid it, one way or another.

Here, the error is that your script is working for more than max_execution_time seconds ; as the error is reported on the mssql_connect line, I suppose the script is waiting for the connection to succeed, and it doesn't get etablished in less that 30 seconds.


I don't have an SQL Server database to test, but looking at the Runtime Configuration section of the manual for mssql, I'd say that these look interesting :

       name               Default value
mssql.connect_timeout        "5"
mssql.timeout                "60"

You could try changing those,

  • either in your php.ini file, if you can modify it
  • or using ini_set() before trying to connect.

In the second case, something like this might do the trick :

ini_set('mssql.connect_timeout', '3');
ini_set('mssql.timeout', '3');
Pascal MARTIN
+1 for looking into configuration. Has to be an php or mysql config problem
vooD
Thanks for the great answer, very detailed. I got the DB code from here http://www.webcheatsheet.com/PHP/connect_mssql_database.php, I have since removed the 'die' bit, and added the @, but it still seems to stop processing the page. It does not output an error, but then nothing else get processed either. I made a simple test: echo ($dbhandle = @mssql_connect($myServer, $myUser, $myPass));I would expect either a true or false result, but nothing gets outputted (and again the page processing stops). With my C# hat on, do I need to add a PHP equiv of ".ToString()" to that line?
AaronM
The @ operator is silencing both the warning, and the errror (it's still happening, but no message is displayed -- hence the script's ending without any other information) ;;; if setting the timeout options doesn't work, that's sad/bad :-(
Pascal MARTIN