Defining a custom error handler for the PHP ADODB Lite database libaryDefining a custom error handler for the PHP ADODB Lite database libary

Posted March 2nd, 2009 in PHP

ADODB Lite is a database abstraction library for PHP. Custom error handlers can be set up for ADODB Lite which will be triggered whenever an error occurs in SQL queries or when attempting to connect to the database. This post looks at how to set up a customer error handler for the ADODB Lite database library.

Defining the error handling function to use with constants

To set up a custom error handler, the ADODB_ERROR_HANDLER_TYPE constant must be defined and set to E_USER_ERROR and the ADODB_ERROR_HANDLER constant set to the name of the function to call. This must be done before the ADODB Lite library files are included.

The following PHP code snippet illustrates this by setting the custom error handler to a function named "my_adodb_errorhandler":

if(!defined('ADODB_ERROR_HANDLER_TYPE')) {
    define('ADODB_ERROR_HANDLER_TYPE', E_USER_ERROR);
}
if(!defined('ADODB_ERROR_HANDLER')) {
    define('ADODB_ERROR_HANDLER', 'my_adodb_errorhandler');
}
require_once('adodb_lite/adodb.inc.php');

The error handling function

The error handling function is shown below, with PHPDOC syntax before the function declaration. In the "// your code here" section you would put whatever code you need to deal with the error such as displaying a custom error page to the website visitor, sending an email to the webmaster, logging the error etc.

/**
* ADODB Error Handler
*
* @param string $dbms The RDBMS you are connecting to, e.g. mysql
* @param string $fn The name of the calling function (in uppercase)
* @param mixed $errno The native error number from the database
* @param string $errmsg The native error msg from the database
* @param string $p1 $fn specific parameter
* @param string $p2 $fn specific parameter
* @param $thisConn $current connection object
*/
//-------------------------------------------------------------------------------------------------
function my_adodb_errorhandler($dbms, $fn, $errno, $errmsg, $p1, $p2, $thisConn) {
//-------------------------------------------------------------------------------------------------

    if($fn == 'CONNECT') {
        // error was connecting to the database
        $errmsg = 'Could not connect to the database server';
    }

    // your code here
   
}

The error handler function parameters

As an example, the following query is run on a MySQL database called "testdb" where the "bar" table does not exist. Then below the query is the example output for each parameter:

SELECT foo FROM bar

The values for each of the parameters would be as follows:

$dbms (the database server type):

mysql

$fn is the function in the ADODB Lite library that was called when the error was triggered. In the case of CONNECT the errormsg may not actually contain anything, hence the section of code in my example function that test fors this. In the above SQL example it would return this:

EXECUTE

$errno (the native error number from MySQL):

1146

$errmsg (the error message direct from MySQL):

Table 'testdb.bar' doesn't exist

$p1 (the actual database query run):

SELECT foo FROM bar

$p2 (other information):

Array
(
)

$thisConn (a handle to the connection object):

Object id #7

Conclusion

Using a database abstraction layer such as ADODB Lite has a number of benefits including being able to easily deal with errors. Every time an error occurs you are able to trigger your own custom error handler by defining it as shown in this post, and take appropriate action.

Related posts:

Share or Bookmark

Share or Bookmark this page using the following services. You will need to have an account with the selected service in order to post links or bookmark this page.

Subscribe or Follow

Subscribe via RSS or email, or follow me on Facebook or Twitter below. The RSS icon takes you through to Feedburner where you can select the service or application to use.

Comments

blog comments powered by Disqus