ADOdb

Database Abstraction Layer for PHP

User Tools

Site Tools


v5:userguide:error_handling

This is an old revision of the document!


Error Handling In ADOdb

Default Behaviour

By default errors in the execution of SQL statements are handled by the database driver that is being accessed by PHP. No errors are normally handled and it is necessary to query the last error code to see if there has been an error.

adodb-errorhandler

  • adodb-error.inc.php and lang/adodb-$lang.inc.php (if you use MetaError())
  • adodb-exceptions.inc.php and adodb-errorhandler.inc.php (if you use adodb error handler or php5 exceptions).

Standard Version

Customization

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

/*
 * Connection to database assumed
*/ 
include("../adodb-exceptions.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. © 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.

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 ($rs) 
    rs2html($rs);

Logging Error Messages

If you want to log the error message, you can do so by defining the following optional constants:

  • ADODB_ERROR_LOG_TYPE
  • ADODB_ERROR_LOG_DEST

ADODB_ERROR_LOG_TYPE is the error log message type (see error_log. In the example below, the value is set to 3, which means log to the file defined by the constant ADODB_ERROR_LOG_DEST.

error_reporting(E_ALL); # report all errors  
ini_set("display_errors", "0"); # but do not echo the 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 ($rs) 
    rs2html($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().

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 ($rs) 
  rs2html($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.

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().

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.

v5/userguide/error_handling.1447212430.txt.gz · Last modified: 2017/04/21 11:39 (external edit)