ADOdb

Database Abstraction Layer for PHP

User Tools

Site Tools


v5:userguide:error_handling

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
userguide:error_handling [2015/11/29 22:17] – [Exception Trapping] mnewnhamv5:userguide:error_handling [2018/06/27 16:18] (current) – function names starting with lowercase (#430) dregad
Line 5: Line 5:
  
 ===== PHP set_error_handler ===== ===== PHP set_error_handler =====
-Once the PHP set_error_handler is used to override the default error handling, then the '@' PHP value ceases to be honored, and any SQL statement execution that fails will then produce a fatal error in the statement. We must then implement an error handling layer to trap the error and process it as the required. The first way to do that is by the use of the ''adodb-errorhandler.inc'' file.  +Once the PHP set_error_handler is used to override the default error handling, then the '@' PHP value ceases to be honored, and any SQL statement execution that fails will then produce a fatal error in the statement. We must then implement an error handling layer to trap the error and process it as the required. The first way to do that is by the use of the ''adodb-errorhandler.inc'' file. 
----------------------------------------+ 
 +--------------------------------------------------- 
 <WRAP right box round> <WRAP right box round>
 == See Also == == See Also ==
-[[reference:metaerror|Meta Error Handling]]+[[v5:reference:connection:metaerror|Meta Error Handling]]
 </WRAP> </WRAP>
 ===== adodb-errorhandler.inc.php ===== ===== adodb-errorhandler.inc.php =====
Line 26: Line 28:
 $result = $db->execute('SELECX * FROM products'); $result = $db->execute('SELECX * FROM products');
 </code> </code>
 +
 +===== error_reporting =====
 +The ADOdb error handler is not dependent on [[http://php.net/manual/en/function.error-reporting.php|PHP Error Reporting]] being set to a value greater than zero. However, the default behaviour of ''adodb-errorhandler.inc.php'' is to respect this setting by returning immediately from the error trap. You can change this by modifying the source of that file.
 +
 +<code php>
 +function ADODB_Error_Handler($dbms, $fn, $errno, $errmsg, $p1, $p2, &$thisConnection)
 +{
 +
 +    /*
 +    * Remark out this line to trap errors when error_reporting = 0
 +    */
 +    if (error_reporting() == 0) 
 +          return; // obey @ protocol
 + 
 +</code>
 +
  
 ===== Override Error Trap/Resume ===== ===== Override Error Trap/Resume =====
Line 49: Line 67:
 set_error_handler('ignoreErrorHandler'); set_error_handler('ignoreErrorHandler');
   
-$db->Execute('DROP INDEX index_that_might_not_exist');+$db->execute('DROP INDEX index_that_might_not_exist');
   
 /* /*
Line 58: Line 76:
  
 </code> </code>
 +
 +===== Using A Class Method =====
 +<code php>
 +class c
 +{
 + function e()
 + {
 + print "ERROR TRAP";
 + }
 +}
 +
 +$c = new c;
 +
 +$db->raiseErrorFn = array($c,'e');
 +</code>
 +
 +===== Combining With Custom Error Handling =====
 +If you use ''adodb-errorhandler.inc.php'', you can still define your own error handling to deal with non-adodb errors. 
 +
 +<code php>
 +error_reporting(E_ALL);
 +
 +function myErrorHandler($p1,$p2,$p3,$p4,$p5)
 +{
 +    /*
 +    * Do some non-adodb handling
 +    */
 +    return true;
 +}
 +
 +set_error_handler('myErrorHandler');
 +
 +include './simple-errorhandler.inc.php';
 +include '../adodb-master/adodb.inc.php';
 +</code>
 +
 +
  
 ===== Exception Trapping ===== ===== Exception Trapping =====
Line 69: Line 124:
 try  try 
 {    {   
-    $db = NewADOConnection("oci8://scott:bad-password@mytns/");   +    $db = newADOConnection("oci8://scott:bad-password@mytns/");   
  
  
Line 80: Line 135:
 </code> </code>
  
- 
-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://php.net/error_reporting|error_reporting]] to control what error messages types to display, and [[http://php.net/trigger_error|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. 
- 
-<code 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 ($rs)  
-    rs2html($rs); 
-</code> 
  
 ===== Logging Error Messages ===== ===== Logging Error Messages =====
Line 125: Line 155:
  
  
-$c = NewADOConnection('mysql'); +$c = newADOConnection('mysql'); 
-$c->PConnect('localhost','root','','northwind');+$c->pConnect('localhost','root','','northwind');
  
-$rs=$c->Execute('select * from productsz'); ## invalid table productsz+$rs=$c->execute('select * from productsz'); ## invalid table productsz
  
 if ($rs)  if ($rs) 
Line 137: 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 'northwind.productsz' doesn't exist] in  EXECUTE("select * from productsz")+  2001-10-28 14:20:38 mysql error: [1146: Table 'northwind.productsz' doesn't exist] 
 +          in  EXECUTE("select * from productsz")
  
 ===== PEAR_ERROR ===== ===== PEAR_ERROR =====
Line 149: Line 180:
 include('tohtml.inc.php'); include('tohtml.inc.php');
  
-$c = NewADOConnection('mysql'); +$c = newADOConnection('mysql'); 
-$c->PConnect('localhost','root','','northwind');+$c->pConnect('localhost','root','','northwind');
  
-$rs=$c->Execute('select * from productsz'); #invalid table productsz');+$rs=$c->execute('select * from productsz'); #invalid table productsz');
  
 if ($rs)  if ($rs) 
Line 177: Line 208:
 ===== MetaError and MetaErrMsg ===== ===== MetaError and MetaErrMsg =====
  
-If you need error messages that work across multiple databases, then use [[reference:metaerror|MetaError()]], which returns a virtualized error number, based on PEAR DB's error number system, and [[reference:metaerrmsg|MetaErrMsg()]].+If you need error messages that work across multiple databases, then use [[v5:reference:connection:metaerror|MetaError()]], which returns a virtualized error number, based on PEAR DB's error number system, and [[v5:reference:connection:metaerrormsg|MetaErrMsg()]].
  
 ===== Error Messages ===== ===== 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.+Error messages are outputted using the static method  
 + 
 +  ADOConnnection::outp($msg,$newline=true).  
 +   
 +By default, it sends the messages to the standard output. You can override this to perform error logging.
v5/userguide/error_handling.1448831829.txt.gz · Last modified: 2017/04/21 11:39 (external edit)