Handling OCI8 Errors : Oracle : Databases PHP Source Code


PHP Source Code » Databases » Oracle »

 

Handling OCI8 Errors


The error handing of any solid application adds complexity and requires careful design.
Expect the unexpected. Check all return codes.
For a start, in a production system you would turn off display_errors in php.ini. You
do not want to leak internal information to web users, and you do not want your application
pages to contain ugly error messages.
For development, enable the E_STRICT level for error_reporting in php.ini so you
can catch any problems that will cause upgrade issues.
To handle errors in OCI8, using the oci_error() function.
For connection errors, no argument is needed:
<?
$c 
oci_connect("hr""hrpwd""//localhost/XE");
if (!
$c) {
   
$e oci_error(); // No parameter passed
   ///Handling OCI8 Errors
   
var_dump($e);
}
///For parse errors, pass the connection resource:
---------
$s oci_parse($c"select city from locations");
if (!
$s) {
$e oci_error($c); // Connection handle passed
var_dump($e);
}
---------
///For execution and fetching errors, pass the statement resource:
$rc oci_execute($s);
if (!
$rc) {
$e oci_error($s); // Statement handle passed
var_dump($e);
}
$rc oci_fetch_all($s$results);
if (!
$rc) {
$e oci_error($s); // Statement handle passed
var_dump($e);
}
//You might consider using PHP&#8217;s @ to suppress function errors. For pretty output, using PHP&#8217;s
//output buffering functions may be a way to help hide errors from users.
function Connect(&$e$un$pw$db)
{
    
ob_start();
    
$con = @oci_connect($un$pw$db);
    if (!
$con) {
     
DisplayOCIError();
     
$e ob_get_contents();
    }
 
ob_end_clean();
 return(
$con);
}
function 
DisplayOCIError($r false)
{
    if (
$r)
        
$err = @oci_error($r);
        else
        
$err = @oci_error();
        echo 
"<p><b>Error</b>:</p>\n";
        echo 
"<pre>\n";
    if (isset(
$err['message'])) {
      echo 
htmlspecialchars($err['message']);
    }
    else {
      echo 
'Unknown DB error';
    }
    echo 
'</pre>'."\n";
    
//---
    
}

?>


HTML code for linking to this page:

Follow Navioo On Twitter

PHP Source Code

 Navioo Databases
» Oracle