v5:userguide:error_handling
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
userguide:error_handling [2015/11/11 04:27] – mnewnham | v5:userguide:error_handling [2018/06/27 16:18] (current) – function names starting with lowercase (#430) dregad | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Error Handling | ====== Error Handling | ||
+ | |||
===== Default Behaviour ===== | ===== 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. | + | 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. This is done by use of the ' |
- | ===== adodb-errorhandler | + | ===== PHP set_error_handler |
- | * adodb-error.inc.php | + | Once the PHP set_error_handler is used to override the default |
- | * adodb-exceptions.inc.php and adodb-errorhandler.inc.php (if you use adodb error handler or php5 exceptions). | + | |
- | ==== Standard Version ==== | + | --------------------------------------------------- |
- | ==== Customization | + | <WRAP right box round> |
- | ADOdb supports PHP5 exceptions. Just include _adodb-exceptions.inc.php_ and you can now catch exceptions | + | == See Also == |
+ | [[v5: | ||
+ | </ | ||
+ | ===== adodb-errorhandler.inc.php ===== | ||
+ | This file is designed to be copied off and customized for your own use. In it's simplest form, it can be used to emulate the ' | ||
<code php> | <code php> | ||
+ | include ' | ||
+ | include ' | ||
+ | |||
+ | $db = newAdoConnection(' | ||
+ | $db-> | ||
+ | |||
/* | /* | ||
- | * Connection to database assumed | + | * Invalid SQL statement will be trapped inside the custom errorhandling file |
- | */ | + | */ |
- | include(" | + | $result |
- | try { | + | |
- | | + | |
- | } catch (exception | + | |
- | var_dump($e); | + | |
- | adodb_backtrace($e->gettrace()); | + | |
- | } | + | |
</ | </ | ||
+ | ===== error_reporting ===== | ||
+ | The ADOdb error handler is not dependent on [[http:// | ||
- | 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 [[http:// | + | < |
+ | function ADODB_Error_Handler($dbms, | ||
+ | { | ||
+ | |||
+ | /* | ||
+ | * Remark out this line to trap errors when error_reporting = 0 | ||
+ | | ||
+ | if (error_reporting() == 0) | ||
+ | return; | ||
+ | |||
+ | </code> | ||
- | Including the above file will cause _trigger_error($errorstring, | ||
- | (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 | + | ===== Override Error Trap/Resume ===== |
+ | In certain circumstances, | ||
- | 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(" | + | <code php> |
+ | /** | ||
+ | * Traps errors and ignores them | ||
+ | */ | ||
+ | function ignoreErrorHandler() | ||
+ | { | ||
+ | return true; | ||
+ | } | ||
+ | |||
+ | /* | ||
+ | * In Oracle we have no 'if index exists' | ||
+ | * so if the index did not exist in the first place, it would throw an error | ||
+ | * We want the error ignored, and for the program | ||
+ | */ | ||
+ | |||
+ | $saveErrorFn = $db-> | ||
+ | $db-> | ||
+ | set_error_handler(' | ||
+ | |||
+ | $db-> | ||
+ | |||
+ | /* | ||
+ | * Set the error handling back | ||
+ | */ | ||
+ | restore_error_handler(); | ||
+ | $db-> | ||
+ | |||
+ | </ | ||
+ | ===== Using A Class Method ===== | ||
<code php> | <code php> | ||
- | error_reporting(E_ALL); # pass any error messages triggered to error handler | + | class c |
+ | { | ||
+ | function e() | ||
+ | { | ||
+ | print "ERROR TRAP"; | ||
+ | } | ||
+ | } | ||
- | include(' | + | $c = new c; |
- | include(' | + | |
- | include(' | + | |
- | $c = NewADOConnection(' | + | $db-> |
- | $c-> | + | |
- | $rs=$c-> | + | |
- | if ($rs) | + | |
- | rs2html($rs); | + | |
</ | </ | ||
+ | |||
+ | ===== Combining With Custom Error Handling ===== | ||
+ | If you use '' | ||
+ | |||
+ | <code php> | ||
+ | error_reporting(E_ALL); | ||
+ | |||
+ | function myErrorHandler($p1, | ||
+ | { | ||
+ | /* | ||
+ | * Do some non-adodb handling | ||
+ | */ | ||
+ | return true; | ||
+ | } | ||
+ | |||
+ | set_error_handler(' | ||
+ | |||
+ | include ' | ||
+ | include ' | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ===== Exception Trapping ===== | ||
+ | ADOdb supports exception trapping using the '' | ||
+ | |||
+ | <code php> | ||
+ | |||
+ | include ' | ||
+ | include ' | ||
+ | |||
+ | try | ||
+ | { | ||
+ | $db = newADOConnection(" | ||
+ | |||
+ | } | ||
+ | catch (exception $e) | ||
+ | { | ||
+ | var_dump($e); | ||
+ | adodb_backtrace($e-> | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
===== Logging Error Messages ===== | ===== Logging Error Messages ===== | ||
Line 70: | Line 155: | ||
- | $c = NewADOConnection(' | + | $c = newADOConnection(' |
- | $c->PConnect(' | + | $c->pConnect(' |
- | $rs=$c->Execute(' | + | $rs=$c->execute(' |
if ($rs) | if ($rs) | ||
Line 82: | Line 167: | ||
The following message will be logged in the error.log file: | The following message will be logged in the error.log file: | ||
- | 2001-10-28 14:20:38 mysql error: [1146: Table ' | + | 2001-10-28 14:20:38 mysql error: [1146: Table ' |
+ | | ||
===== PEAR_ERROR ===== | ===== PEAR_ERROR ===== | ||
Line 94: | Line 180: | ||
include(' | include(' | ||
- | $c = NewADOConnection(' | + | $c = newADOConnection(' |
- | $c->PConnect(' | + | $c->pConnect(' |
- | $rs=$c->Execute(' | + | $rs=$c->execute(' |
if ($rs) | if ($rs) | ||
Line 122: | Line 208: | ||
===== MetaError and MetaErrMsg ===== | ===== MetaError and MetaErrMsg ===== | ||
- | If you need error messages that work across multiple databases, then use [[reference: | + | If you need error messages that work across multiple databases, then use [[v5:reference:connection: |
===== Error Messages ===== | ===== Error Messages ===== | ||
- | Error messages are outputted using the static method ADOConnnection:: | + | Error messages are outputted using the static method |
+ | |||
+ | | ||
+ | |||
+ | By default, it sends the messages to the standard output. You can override this to perform error logging. |
v5/userguide/error_handling.1447212430.txt.gz · Last modified: 2017/04/21 11:39 (external edit)