PHP Error handling in WordPress plugin

Posted by AaronM on Stack Overflow See other posts from Stack Overflow or by AaronM
Published on 2010-04-03T11:28:44Z Indexed on 2010/04/03 11:33 UTC
Read the original article Hit count: 357

Filed under:
|
|
|

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

© Stack Overflow or respective owner

Related posts about Wordpress

Related posts about plugin