Hi All,
Im trying to learn how to use pdo, and am curious to know how i best can deal with allkind off errors. Lets say there is a typo, or im selecting an non existing row etc etc.
In my connection, do I have do set anyting different here:
Class PDO_CONNECTION extends PDO
{
public function __construct(
$DSN, $USER = '', $PASS = '', $DRIVER_OPTIONS = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION) )
{
// OPRET PDO OBJECT FRA PARENT PDO CLASS :
parent::__construct( $DSN, $USER, $PASS, $DRIVER_OPTIONS );
}
}
When I run this query, is this the "best way"to run a query, in a try catch block? Should i allways do that?
(The connection is included in the construct that is also in the class as the method below)
public function create( $cat, $ref, $price, $thumb, $large )
{
try {
$stmt = $this->database->prepare("INSERT INTO products
( cat, ref, price, img_small, img_large )
VALUES
( :cat, :ref, :price, :thumb, :large )");
// Execute :
if( $stmt->execute(array(
':cat' => $cat,
':ref' => $ref,
':price' => $price,
':thumb' => $thumb,
':large' => $large ))) {
return true;
}
else {
return false;
}
}
catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
}
}
**** Does catch mean, that I can avoid outputting all the ugly err messages, and perhaps log errors in a txt file or whatever else i would prefer? Havent used try/catch before, so forgive my questions :-)
Best, Klemme