PDO Exception Handling Question

PDO Exception Handling Question

am 02.04.2006 09:53:00 von Rodney Topor

I've just started using PDO with PHP 5.1 and MySQL 5.0. Everything
seems to work fine, except for one issue with the following example
from the PHP manual.

try {
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
foreach ($dbh->query('SELECT * from FOO') as $row) {
print_r($row);
}
$dbh = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "
";
die();
}
?>

If an error occurs in creating the new PDO instance, or in issuing the
PDO query, then a PDOException should be raised. But nothing is ever
printed. I've tried running this script, forcing an exception, both
from the command line and from the server. But nothing is ever
printed. What do I have to do to see the printed string in the
catch-block?

Rodney Topor

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

Re: PDO Exception Handling Question

am 02.04.2006 14:00:42 von Chris Smith

On 4/2/06, Rodney Topor wrote:
> I've just started using PDO with PHP 5.1 and MySQL 5.0. Everything
> seems to work fine, except for one issue with the following example
> from the PHP manual.
>
> > try {
> $dbh =3D new PDO('mysql:host=3Dlocalhost;dbname=3Dtest', $user, $pass=
);
> foreach ($dbh->query('SELECT * from FOO') as $row) {
> print_r($row);
> }
> $dbh =3D null;
> } catch (PDOException $e) {
> print "Error!: " . $e->getMessage() . "
";
> die();
> }
> ?>
>
> If an error occurs in creating the new PDO instance, or in issuing the
> PDO query, then a PDOException should be raised. But nothing is ever
> printed. I've tried running this script, forcing an exception, both
> from the command line and from the server. But nothing is ever
> printed. What do I have to do to see the printed string in the
> catch-block?

Do you mean it doesn't print anything or it doesn't get into the catch bloc=
k?

ie it prints "Error!:
" or it doesn't get into that section at all?

--
Postgresql & php tutorials
http://www.designmagick.com/

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

Re: PDO Exception Handling Question

am 02.04.2006 23:11:37 von Rodney Topor

I did some more checking. If there is an error in the "new PDO(...)"
constructor call, then it does get into the catch block and print an
appropriate error message. (Good. Perhaps I was previously mistaken.)

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?

On Sun, 2 Apr 2006, chris smith wrote:

> On 4/2/06, Rodney Topor wrote:
>> I've just started using PDO with PHP 5.1 and MySQL 5.0. Everything
>> seems to work fine, except for one issue with the following example
>> from the PHP manual.
>>
>> >> try {
>> $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
>> foreach ($dbh->query('SELECT * from FOO') as $row) {
>> print_r($row);
>> }
>> $dbh = null;
>> } catch (PDOException $e) {
>> print "Error!: " . $e->getMessage() . "
";
>> die();
>> }
>> ?>
>>
>> If an error occurs in creating the new PDO instance, or in issuing the
>> PDO query, then a PDOException should be raised. But nothing is ever
>> printed. I've tried running this script, forcing an exception, both
>> from the command line and from the server. But nothing is ever
>> printed. What do I have to do to see the printed string in the
>> catch-block?
>
> Do you mean it doesn't print anything or it doesn't get into the catch block?
>
> ie it prints "Error!:
" or it doesn't get into that section at all?
>
> --
> Postgresql & php tutorials
> http://www.designmagick.com/
>

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

Re: PDO Exception Handling Question

am 03.05.2006 16:42:33 von Cyruss

> 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);

// 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() . "
";
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() . "
";

}


?>

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

Re: PDO Exception Handling Question

am 03.05.2006 16:45:21 von Cyruss

And also my question is :

"Why did the PDO creators (Wez the King?) decide to choose the silent
mode for being the default one ?"

Why not defining the ERRMODE_WARNING by default ?

Or perhaps I misunderstand something, and I would be very happy to learn
a bit :)

Thx

Cyril

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

Re: PDO Exception Handling Question

am 03.05.2006 17:02:47 von Cyruss

Read on otn :

"PDO_ERRMODE_SILENT
This is the default mode; it will simply set the error code for you to
inspect using the errorCode() and errorInfo() methods of both the
statement and database handle objects.

if (!$dbh->exec($sql)) {
echo $dbh->errorCode() . "
";
$info = $dbh->errorInfo();
// $info[0] == $dbh->errorCode() unified error code
// $info[1] is the driver specific error code
// $info[2] is the driver specific error string"




Cyril PIERRE de GEYER a écrit :
> And also my question is :
>
> "Why did the PDO creators (Wez the King?) decide to choose the silent
> mode for being the default one ?"
>
> Why not defining the ERRMODE_WARNING by default ?
>
> Or perhaps I misunderstand something, and I would be very happy to learn
> a bit :)
>
> Thx
>
> Cyril

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

Re: PDO Exception Handling Question

am 20.04.2007 16:50:01 von Jerry Schwartz

I hope by now you figured this out, but you have to use $dbh->setAttribute()
to enable exception handling for anything but the initial object creation.

Worse yet, as of 2007/04/20 the return values of setAttribute are reversed.
It returns TRUE on failure and FALSE on success.

"Rodney Topor" wrote in message
news:Pine.LNX.4.64.0604030708070.2078@hobbit.cit.gu.edu.au.. .
>I did some more checking. If there is an error in the "new PDO(...)"
> constructor call, then it does get into the catch block and print an
> appropriate error message. (Good. Perhaps I was previously mistaken.)
>
> 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?
>
> On Sun, 2 Apr 2006, chris smith wrote:
>
>> On 4/2/06, Rodney Topor wrote:
>>> I've just started using PDO with PHP 5.1 and MySQL 5.0. Everything
>>> seems to work fine, except for one issue with the following example
>>> from the PHP manual.
>>>
>>> >>> try {
>>> $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
>>> foreach ($dbh->query('SELECT * from FOO') as $row) {
>>> print_r($row);
>>> }
>>> $dbh = null;
>>> } catch (PDOException $e) {
>>> print "Error!: " . $e->getMessage() . "
";
>>> die();
>>> }
>>> ?>
>>>
>>> If an error occurs in creating the new PDO instance, or in issuing the
>>> PDO query, then a PDOException should be raised. But nothing is ever
>>> printed. I've tried running this script, forcing an exception, both
>>> from the command line and from the server. But nothing is ever
>>> printed. What do I have to do to see the printed string in the
>>> catch-block?
>>
>> Do you mean it doesn't print anything or it doesn't get into the catch
>> block?
>>
>> ie it prints "Error!:
" or it doesn't get into that section at all?
>>
>> --
>> Postgresql & php tutorials
>> http://www.designmagick.com/
>>

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