WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : A Custom Error Handling And Debugging Class
Categories : PHP, PHP Classes, Debugging, Errors and Logging Update Picture
Rahul Puranik
Date : Apr 22nd 2005
Grade : 1 of 5 (graded 7 times)
Viewed : 8056
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Rahul Puranik
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

<?
/*******************************************************************************
Program Name     : Error.php
Purpose         : This file contains the error class,which manages errors.
                  It handles erros in customized way
Creation Date     : 04/22/2005 02:05PM
********************************************************************************/
class Error
{
    var
$logfile;
    var
$debug;
   
/*==============================================================================
Constructor
Arguments :- $debug_yesno. If 1 then errors will be logged,else displayed
==============================================================================*/
   
function Error($debug_yesno=0,$logfile_path)
    {
       
$this->debug=$debug_yesno;
       
$this->printdebug=1;

       
//Report only user generated error messages
       
error_reporting(E_USER_ERROR|E_USER_WARNING|E_USER_NOTICE);     
       
        if(
$this->debug>=1)
        {
           
$this->logfile=$logfile_path;
           
set_error_handler(array($this,"ErrorHandler"));
        }
        else
        {
           
restore_error_handler();
        }
    }       
   
/*==============================================================================
Following function logs an error message
==============================================================================*/
   
function ErrorHandler($errno,$errstr,$errfile,$errline)
    {
           
$errtime=date("H:i:s - d/m/y");
           
$error_message="An error occured on ".$errtime."\n";
           
$error_message .="Details are as follows\n";
           
$error_message .="Error Number : $errno\n";
           
$error_message .=$errstr."\nOccured in ".$errfile."\n";
           
$error_message .="On Line $errline \n\n";
           
$error_message .=str_repeat("-",50);
           
$error_message .="\n";
           
$fp=fopen($this->logfile,"r+");
           
fwrite($fp,$error_message);
           
fclose($fp);
    }

/*==============================================================================
Following function prints debug messages on the screen
==============================================================================*/
   
function PrintDebug($message,$yesno=0)
    {
        if(
$yesno>0)
        {
           
$message="<font face=\"verdana\" size=\"2\" color=\"red\">Debug : ".$message."</font><br>";
            print
$message;
        }
    }
}
?>




How to use this class in the script.
Initialize this class as $error=new Error(1,"/err.log"). Constructor argument
value "1" will set the custom error handler and redirect all errors to a log
file specified by the second argument "/err.log".In your script,whereever you
expect an error to happen, use PHP's builtin function - trigger_error("Your
error message") to trigger error.You won't see any other error or warning except
parse errors with this class.

In PHP scripts we often need to "echo" certain variables and then delete those
echo statements for debugging pupose. Instead of that we can use
$error->PrintDebug("debug message",$yesno). This will provide a neat error
message and also will eliminate the need of deleting those messages. To print
selectively, call function with second argument with value 1. Like following
$error->PrintDebug("This is the message",1);

There are a lot of enhacements to be done for this class.

<?
//If initialized with 1 errors will be logged. else they will be displayed on the screen
$error=new Error(1,"/err.log");

$conn=mysql_connect($host,$username,$password);

if(!
$conn)
{
   
trigger_error("Couldn't Connect To MySQL Because Of Following Error :".MySQL_Error());
    die();
}
else
{
   
$error->PrintDebug("MySQL Connected",1);
   
   
$dblink=mysql_select_db($dbname);
    if(!
$dbname)
    {
       
trigger_error("Couldn't Select Database For Following Error".MySQL_Error());
        die();
    }   
    else
    {
       
$error->PrintDebug("Database Selected",1);
    }
}
?>



[PHP5] PHP Debugger and Helper
Categories : PHP, PHP Classes, Errors and Logging, Debugging, XML
The class to check load time of your script VERY usefull for relatively slow applications, but not only..
Categories : PHP, PHP Classes, Debugging
DB Connection Function with error handling and email failure notices
Categories : PHP, MySQL, Errors and Logging, Databases, Errors and Logging
A time measuring and performance benchmarking class
Categories : PHP, PHP Classes, Testing, Debugging, Date Time
Building a basic error handler with custom error types
Categories : PHP, PHP Classes, Errors and Logging
Example of function to send out email if error occurs
Categories : PHP, Email, Debugging, Errors and Logging
file class , uploade file , download file already uploaded on another website
Categories : PHP, PHP Classes, Filesystem, Web Services
Authorize.net AIM Interface Class v1.0.0
Categories : PHP, PHP Classes, Ecommerce, Payment Gateways
crop and resize image class using gd library function
Categories : PHP, PHP Classes, GD image library, Graphics
News management class
Categories : PHP, PHP Classes, Beginner Guides
A Timing Class
Categories : PHP, PHP Classes, Date Time
Expose - PHP template engine, supports server and client-sided caching,a plugin system, multiple languages, template script language is based on PHP itself.
Categories : PHP, PHP Classes, Templates, Complete Programs
Create HTML forms dynamicly using Javascript & PHP
Categories : PHP, PHP Classes, Java Script
PhpView 0.1 - simple php viewer, using temporary files and frames.
Categories : PHP, PHP Options and Info, Debugging, HTML and PHP
RSS parser. Parses RSS into an array. Quick and nasty but does the job. No checking is done for correct Tags, only correct XML. PHP4 needed to display result (uses print_r).
Categories : PHP, XML, PHP Classes, Rich Site Summary (RSS)