I'm a newbie to both PHP and Wordpress (but do ok in C#), and am battling to know the mistake handling inside a custom wordpress plugin I'm trying to create. The fundamentals from the wordpress plugin would be to query an exsiting MSSQL database (note it is not the conventional MYSQL db...) and return the rows to the screen. It was working well, however the host company has had my database offline, which brought me towards the error handling problem (that we thought was ok).

The next code is neglecting to connect with the database (not surprisingly), but puts a mistake to the screen and stops the page processing. It doesn't even output the 'or die' error text.

QUESTION: How do i just output an easy "Cant load data" message, and carry 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 produced: (line 31 is $dbhandle = mssql_connect...)

Warning: mssql_connect() [function.mssql-connect]: Not able for connecting to server: <servername> in <file path> online 31

Fatal error: Maximum execution duration of thirty seconds exceeded in <file path> online 31

To begin with, if [cde] boosts an alert when there is a problem, there's very little that you can do to prevent it : the only real factor you could do this is hide it, while using [cde] operator :

mssql_connect

Note : you shouldn't @ whenever a connection error happens : it'll steer clear of the execution from the whole application, that is not necessarily preferred.


The Fatal Error is another problem (that is most likely due to the first).

Observe that you can't get over a Fatal Error : it's Fatal. And that means you must cure it, some way.

Here, the mistake is your script is working in excess of [cde] seconds because the error is reported around the if (($dbhandle = @mssql_connect($myServer, $myUser, $myPass)) === false) { // connection failed } line, I guess the script delays for that link with succeed, also it does not get etablished in less that thirty seconds.


I do not come with an SQL Server database to check, but searching in the Runtime Configuration portion of the manual for mssql, I'd state that these look interesting :

die()

You could attempt altering individuals,

  • in both your php.ini file, if you're able to modify it
  • or using [cde] prior to trying for connecting.

Within the second situation, something similar to this may have the desired effect :

max_execution_time