views:

66

answers:

1

Hi, I'm trying to work with PDO class on php but I have some trouble to find the right way to handle errors, I've wrote this code:

<?php
// $connection alreay created on a class which works with similar UPDATE statements
// I've simply added here trim() and PDO::PARAM... data type


$id = 33;
$name = "Mario Bros.";
$url = "http://nintendo.com";
$country = "jp";


try {

$sql = "UPDATE table_users SET name = :name, url = :url, country = :country WHERE user_id = :user_id";

$statement = $connection->prepare ($sql);

$statement->bindParam (':user_id', trim($id), PDO::PARAM_INT);
$statement->bindParam (':name', trim($name), PDO::PARAM_STR);
$statement->bindParam (':url', trim($url), PDO::PARAM_STR);
$statement->bindParam (':country', trim($country), PDO::PARAM_STR, 2);

$status = $statement->execute ();

} catch (PDOException $e) {
    print $e->getMessage ();
}

print $status; // it returns a null value, and no errors are reported

?>

this portion of code doesn't report errors, but it simply doesn't work, the var $status at the bottom, return a null value.

can someone help me to find where I'm wrong?

+1  A: 

PDO won't throw exceptions unless you tell it to. Have you run:

$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

on the PDO object?

Matchu
simple and fast! thanks
Vittorio Vittori
@Matchu: This is the default behavior? Since when?!
Alix Axel
Since I last built a database class a month ago or so. At least in my environment, silent mode was default.
Matchu
Found it - http://us3.php.net/manual/en/pdo.error-handling.php - "PDO::ERRMODE_SILENT - This is the default mode."
Matchu
@Matchu: Yeah you're right, thanks. Still **`PDO::__construct()` throws a PDOException if the attempt to connect to the requested database fails**.
Alix Axel
Ooh, that's worth knowing. Thanks!
Matchu