Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP PDO Errors and Error Handling

PHP PDO Errors and Error Handling

PHP PDO Reference ManualPHP PDO Reference Manual

Creating a PDO Instance and Setting the Error Mode



setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

} catch (PDOException $e) {

    echo 'Connection failed: ' . $e->getMessage();

}



?>

Note: Regardless of whether PDO::ATTR_ERRMODE is currently set, if the connection fails, PDO::__construct() will always throw a PDOException. An uncaught exception is fatal.

Creating a PDO Instance and Setting the Error Mode in the Constructor



 PDO::ERRMODE_WARNING));

} catch (PDOException $e) {

    echo 'Connection failed: ' . $e->getMessage();

    exit;

}



// This will cause PDO to throw an E_WARNING level error, not an exception (when the data table does not exist)

$dbh->query("SELECT wrongcolumn FROM wrongtable");

?>

The above example will output:



Warning: PDO::query(): SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.wrongtable' doesn't exist in

/tmp/pdo_test.php on line 18

add a note add a note


PHP PDO Reference ManualPHP PDO Reference Manual