I have a PHP script with two deliberate typo mistakes in the statement for an SQL query:
try
{
$stmt = $dbh->prepare("SELECT COUNT(*) FROM Product WHERE non-existent_column=?");
$stmt->blindValue(1, $id, PDO::PARAM_INT);
$stmt->execute();
$row = $stmt->fetchColumn();
}
catch(PDOException $err)
{
var_dump($err->getMessage());
var_dump($dbh->errorInfo());
}
However, the script does not catch the error even after setting attribute to PDO::ERRMODE_EXCEPTION
. What am I missing here?
UPDATE:
This is the full script. The second typo blindValue
has been reverted back. The error remains uncaught:
<?php
$user= "user";
$password = "password";
$dsn = "mysql:dbname=Catalogue;host=localhost";
$dbh = new PDO($dsn, $user, $password);
$dbh->setAttribute(PDO::ERRMODE_EXCEPTION);
$id = 1000;
try
{
$stmt = $dbh->prepare("SELECT COUNT(*) FROM Product WHERE non-existent_column=?");
$stmt->bindValue(1, $id, PDO::PARAM_INT);
$stmt->execute();
$row = $stmt->fetchColumn();
}
catch(PDOException $err)
{
echo "caught";
var_dump($err->getMessage());
var_dump($dbh->errorInfo());
exit();
}
var_dump($stmt);
var_dump($row);
echo "uncaught";
exit();
?>
To catch a PDOException object and handle the associated error: Wrap the call to the PDO constructor in a try block. Following the try block, include a catch block that catches the PDOException object.
PHP Data Objects (or PDO ) are a collection of APIs and interfaces that attempt to streamline and consolidate the various ways databases can be accessed and manipulated into a singular package. Thus, the PDOException is thrown anytime something goes wrong while using the PDO class, or related extensions.
Because exceptions are objects, they all extend a built-in Exception class (see Throwing Exceptions in PHP), which means that catching every PHP exception thrown is as simple as type-hinting the global exception object, which is indicated by adding a backslash in front: try { // ... } catch ( \Exception $e ) { // ... }
PDO is an acronym for PHP Data Objects. PDO is a lean, consistent way to access databases. This means developers can write portable code much easier. PDO is not an abstraction layer like PearDB. PDO is a more like a data access layer which uses a unified API (Application Programming Interface).
Your call to setAttribute() lacks the first parameter:
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
If you didn't get a
Warning: PDO::setAttribute() expects exactly 2 parameters, 1 given
your error_reporting level is too low for a development server and/or you didn't keep an eye on the error log or didn't set display_errors=On (which ever you prefer; I prefer the error log over display_errors).
edit: please try
<?php
echo 'php version: ', phpversion(), "\n";
try {
$dbh = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly');
echo 'client version: ', $dbh->getAttribute(PDO::ATTR_CLIENT_VERSION), "\n";
echo 'server version: ', $dbh->getAttribute(PDO::ATTR_SERVER_VERSION), "\n";
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch(PDOException $err) {
var_dump($err->getMessage());
die('...');
}
$id = 'foo';
try
{
$stmt = $dbh->prepare("SELECT COUNT(*) FROM Product WHERE `non-existent_column`=?");
$stmt->bindValue(1, $id, PDO::PARAM_INT);
$stmt->execute();
$row = $stmt->fetchColumn();
}
catch(PDOException $err)
{
var_dump($err->getMessage());
var_dump($dbh->errorInfo());
die('....');
}
echo 'done.';
printed on my machine
php version: 5.3.5
client version: mysqlnd 5.0.7-dev - 091210 - $Revision: 304625 $
server version: 5.5.8
string(94) "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'non-existent_column' in 'where clause'"
array(3) {
[0]=>
string(5) "42S22"
[1]=>
int(1054)
[2]=>
string(54) "Unknown column 'non-existent_column' in 'where clause'"
}
....
$stmt->blindValue(1, $id, PDO::PARAM_INT);
This should be $stmt->bindValue(1, $id, PDO::PARAM_INT);
You cannot catch Fatal Errors such as calling an undefined function/method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With