WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
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 Resources
Web Development Content
Internet Security Software
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
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

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 : 16629
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  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
Example of function to send out email if error occurs
Categories : PHP, Email, Debugging, Errors and Logging
DB Connection Function with error handling and email failure notices
Categories : PHP, MySQL, Errors and Logging, Databases, Errors and Logging
The class to check load time of your script VERY usefull for relatively slow applications, but not only..
Categories : PHP, PHP Classes, Debugging
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
Class for sending mail with MIME attachments in multipart format using external sendmail, mimencode and zip
Categories : Email, Network, PHP, PHP Classes
Browser Detecor Class
Categories : PHP Classes, PHP, HTML
filesplit : Split big text files in multiple small ones
Categories : PHP, Log Files, Filesystem, PHP Classes
A File Browser Class.To Read Drives,Directories and Files .Files writing is also possible
Categories : PHP, PHP Classes, Filesystem
an example of the cyberlib payment class
Categories : PHP, PHP Classes, Ecommerce, Credit Cards
[PHP5] aDB PDO LIKE Database Abstraction. Switch easily from one db server to another, strong errors management, manage transactions, queries preparation and more.
Categories : PHP, PHP Classes, Databases, MS SQL Server, MySQL
Filter - A simple class that lets you use multiple functions to create custom filters.
Categories : PHP, PHP Classes, Strings
Remote Archive (Zip, Tar, Gzip) downloader with FTP and local extration support
Categories : PHP, FTP, Filesystem, PHP Classes, Compression
DbObject - A PHP wrapper for working with various databases
Categories : Databases, PHP, PHP Classes