PDO: Error Mode Attributes


PHP Data Objects (PDO) Step By Step Tutorial - Part 14: We will talk about PDO::ERRMODE. This attribute controls the error reporting mode. It have three value: PDO::ERRMODE_SILENT, PDO::ERRMODE_WARNING, and PDO:ERRMODE_EXCEPTION. Ok, let's talk about them.

PDO::ERRMODE_SILENT: when there is error, no action is taken. The error codes are available via PDO::errorCode() and PDO::errorInfo(). It is default value for PDO::ATTR_ERRMODE.

<?php
// configuration
$dbtype		= "sqlite";
$dbhost 	= "localhost";
$dbname		= "test";
$dbuser		= "root";
$dbpass		= "admin";

// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);

// query
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
$sql = "SELECT * FROM booksa";
$q = $conn->query($sql) or die("ERROR: " . implode(":", $conn->errorInfo()));

$r = $q->fetch(PDO::FETCH_ASSOC);

print_r($r);

//result:
//ERROR: 42S02:1146:Table 'test.booksa' doesn't exist
?>

PDO::ERRMODE_WARNING: No action is taken, but an error will be raised with E_WARNING level.

// query
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$sql = "SELECT * FROM booksa";
$q = $conn->query($sql) or die("ERROR: " . implode(":", $conn->errorInfo()));

$r = $q->fetch(PDO::FETCH_ASSOC);

print_r($r);

//result:
//Warning: PDO::query() [function.PDO-query]: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.booksa' doesn't exist in 
//C:\AppServ5\www\test\pdo\test.php on line 15
//ERROR: 42S02:1146:Table 'test.booksa' doesn't exist

PDO::ERRMODE_EXCEPTION: will set the error codes (as with PDO::ERRMODE_SILENT) and en exception of class PDOException will be thrown.

// query
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM booksa";
$q = $conn->query($sql) or die("ERROR: " . implode(":", $conn->errorInfo()));

$r = $q->fetch(PDO::FETCH_ASSOC);

print_r($r);

//result:
//Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.booksa' doesn't exist' in 
//C:\AppServ5\www\test\pdo\test.php:15 Stack trace: #0 C:\AppServ5\www\test\pdo\test.php(15): PDO->query('SELECT * FROM b...') #1 {main} thrown in 
//C:\AppServ5\www\test\pdo\test.php on line 15

Previous: PDO: Setting Connection Attributes
Next: PDO: Improve Performance with Persistent Connection



Series this article:
PDO: Introduction PHP Data Object
PDO: Activation PHP Data Objects Extension
PDO: Connecting Use PHP Data Object
PDO: Portable Connection to Database
PDO: Posibble Fetch Mode
PDO: Error Handling
PDO: Prepared Statement
PDO: Positional and Named Placeholders
PDO: Insert and Update Statement Use Prepared Statement
PDO: Prepared Statement and Bound Values
PDO: Working With BLOBs
PDO: Alternative Retrieve BLOB Data
PDO: Setting Connection Attributes
PDO: Error Mode Attributes
PDO: Improve Performance with Persistent Connection
PDO: Getting Connection Attributes

Bookmark and Share Tag: PHP Data Objects, PDO, extension, PHP extension, connection attributes, error mode Category: PHP Database Post : April 20th 2008 Read: 65,388

advertisements


blog comments powered by Disqus