Re: PDO Exception Handling Question

From: Date: Wed, 03 May 2006 14:42:33 +0000
Subject: Re: PDO Exception Handling Question
References: 1 2 3  Groups: php.db 
Request: Send a blank email to php-db+get-41802@lists.php.net to get a copy of this message
But if there is an error in the "SELECT" query (either syntax error, or reference to missing table), then no exception is raised, and control just continues in the try block with a null result from the query. Is this the intended behaviour?
Hey, it's really fun I was looking here and on internals to ask quite the same thing. In fact what I have found is that PDO error default handler is silent. But you can change it using : $dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); or $dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING); <?php // Connexion parameter $user = 'cyril'; $pass = 'motdepasse'; $dsn = 'mysql:host=localhost;dbname=testPDO'; // Connexion try { $dbh = new PDO($dsn, $user, $pass); } catch (PDOException $e) { print "Erreur ! : " . $e->getMessage() . "<br/>"; die(); } $dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); $dbh->beginTransaction(); try { $sql = "INSERT INTO produit (idproduit, nom, marque, prix) VALUES (NULL,'CB500', 'Honda', '6000')"; $dbh->exec($sql); $idInsere = $dbh->lastInsertId(); $sql2 = "INSERT INTO disponibilite (idproduit, quantite) VALUES ('$idInsere', 5)"; $dbh->exec($sql2); $dbh->commit(); }catch (PDOException $e){ $dbh->rollBack(); print "Erreur ! : " . $e->getMessage() . "<br/>"; } ?>

« previous php.db (#41802) next »