Re: PDO Exception Handling Question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




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/>";
	
}


?>

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux