ADOdb Library for PHP Manual
Prev Using Custom Error Handlers and PEAR_Error
Next

Using Custom Error Handlers and PEAR_Error

ADOdb supports PHP5 exceptions. Just include adodb-exceptions.inc.php and you can now catch exceptions on errors as they occur.

	include("../adodb-exceptions.inc.php"); 
	include("../adodb.inc.php");	 
	try { 
		$db = NewADOConnection("oci8://scott:bad-password@mytns/"); 
	} catch (exception $e) { 
		var_dump($e); 
		adodb_backtrace($e->gettrace());
	} 

ADOdb also provides two custom handlers which you can modify for your needs. The first one is in the adodb-errorhandler.inc.php file. This makes use of the standard PHP functions error_reporting to control what error messages types to display, and trigger_error which invokes the default PHP error handler.

Including the above file will cause trigger_error($errorstring,E_USER_ERROR) to be called when
(a) Connect() or PConnect() fails, or
(b) a function that executes SQL statements such as Execute() or SelectLimit() has an error.
(c) GenID() appears to go into an infinite loop.

The $errorstring is generated by ADOdb and will contain useful debugging information similar to the error.log data generated below. This file adodb-errorhandler.inc.php should be included before you create any ADOConnection objects.

If you define error_reporting(0), no errors will be passed to the error handler. If you set error_reporting(E_ALL), all errors will be passed to the error handler. You still need to use ini_set("display_errors", "0" or "1") to control the display of errors.

<?php
    error_reporting
(E_ALL); # pass any error messages triggered to error handler
    include('adodb-errorhandler.inc.php');
    include('adodb.inc.php');
    include('tohtml.inc.php');
    $c NewADOConnection('mysql');
    $c->PConnect('localhost','root','','northwind');
    $rs=$c->Execute('select * from productsz'); #invalid table productsz');
    if ($rsrs2html($rs);
?>

If you want to log the error message, you can do so by defining the following optional constants ADODB_ERROR_LOG_TYPE and ADODB_ERROR_LOG_DEST. ADODB_ERROR_LOG_TYPE is the error log message type (see error_log in the PHP manual). In this case we set it to 3, which means log to the file defined by the constant ADODB_ERROR_LOG_DEST.

<?php
    error_reporting
(E_ALL); # report all errors
    
ini_set("display_errors", "0"); # do not echo any errors

    define('ADODB_ERROR_LOG_TYPE',3);
    define('ADODB_ERROR_LOG_DEST','C:/errors.log');
    include('adodb-errorhandler.inc.php');
    include('adodb.inc.php');
    include('tohtml.inc.php');

    $c NewADOConnection('mysql');
    $c->PConnect('localhost','root','','northwind');
    $rs=$c->Execute('select * from productsz'); ## invalid table productsz
    if ($rsrs2html($rs);
?>

The following message will be logged in the error.log file:

(2001-10-28 14:20:38) mysql error: [1146: Table 'northwind.productsz' doesn't exist] in EXECUTE("select * from productsz")

PEAR_ERROR

The second error handler is adodb-errorpear.inc.php. This will create a PEAR_Error derived object whenever an error occurs. The last PEAR_Error object created can be retrieved using ADODB_Pear_Error().

<?php
    
include('adodb-errorpear.inc.php');
    include(
'adodb.inc.php');
    include(
'tohtml.inc.php');
    
$c NewADOConnection('mysql');
    
$c->PConnect('localhost','root','','northwind');
    
$rs=$c->Execute('select * from productsz'); #invalid table productsz');
    
if ($rsrs2html($rs);
    else {
        
$e ADODB_Pear_Error();
        echo 
'<p>',$e->message,'</p>';
    }
?>

You can use a PEAR_Error derived class by defining the constant ADODB_PEAR_ERROR_CLASS before the adodb-errorpear.inc.php file is included. For easy debugging, you can set the default error handler in the beginning of the PHP script to PEAR_ERROR_DIE, which will cause an error message to be printed, then halt script execution:

    include('PEAR.php');
    
PEAR::setErrorHandling('PEAR_ERROR_DIE');

Note that we do not explicitly return a PEAR_Error object to you when an error occurs. We return false instead. You have to call ADODB_Pear_Error() to get the last error or use the PEAR_ERROR_DIE technique.

Error Messages

Error messages are outputted using the static method ADOConnnection::outp($msg,$newline=true). By default, it sends the messages to the client. You can override this to perform error-logging.

MetaError and MetaErrMsg

If you need error messages that work across multiple databases, then use MetaError(), which returns a virtualized error number, based on PEAR DB's error number system, and MetaErrMsg().


Prev Home Next
Recordset Filters Up Data Source Names

Sponsored by phpLens