Problem with PDO exceptions

Hello,

I have MySQL 5.1 and PHP 5.2. For some reason PDO is not throwing
exceptions when I give it a broken SQL query. For example:

try {
$stmt = $db->prepare("SELECT * FROM foobar WHERE 1");
} catch(PDOException $e) {
error($e->getMessage());
}

In this example there is no table called 'foobar', so this should give
an error. Yet, it doesn't.


Any ideas why that would happen? Possible causes I can think of:
1) PDO decide to "emulate" prepared statements.
2) PDO has some sort of "errors off" setting.
3) My try-catch statement is wrong.


I doubt it's (1) because I have MySQL 5.1 and PDO is only supposed to
emulate prepared statements for MySQL versions prior to 4.1. And phpinfo
says that I'm running pdo_mysql version 5.1.30.

Btw, $db->exec() and $db->query() also fail to produce an error when I
give them a broken query. That's another reason why I think that the
problem is elsewhere.

Any ideas?

Thank you very much for your help.

Best,
Daniel.

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Daniel Carrera [ Sa, 07 März 2009 20:44 ] [ ID #1992571 ]

Re: Problem with PDO exceptions

I think it is a bug. I have seen this happening at work (PDO not
throwing exception when executing a query on a non existing table)

News on iPhone: http://trk7.com/mob

On 08/03/2009, at 6:44 AM, Daniel Carrera
<daniel.carrera [at] theingots.org> wrote:

> Hello,
>
> I have MySQL 5.1 and PHP 5.2. For some reason PDO is not throwing
> exceptions when I give it a broken SQL query. For example:
>
> try {
> $stmt = $db->prepare("SELECT * FROM foobar WHERE 1");
> } catch(PDOException $e) {
> error($e->getMessage());
> }
>
> In this example there is no table called 'foobar', so this should
> give an error. Yet, it doesn't.
>
>
> Any ideas why that would happen? Possible causes I can think of:
> 1) PDO decide to "emulate" prepared statements.
> 2) PDO has some sort of "errors off" setting.
> 3) My try-catch statement is wrong.
>
>
> I doubt it's (1) because I have MySQL 5.1 and PDO is only supposed
> to emulate prepared statements for MySQL versions prior to 4.1. And
> phpinfo says that I'm running pdo_mysql version 5.1.30.
>
> Btw, $db->exec() and $db->query() also fail to produce an error when
> I give them a broken query. That's another reason why I think that
> the problem is elsewhere.
>
> Any ideas?
>
> Thank you very much for your help.
>
> Best,
> Daniel.
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Kesavan Rengarajan [ Sa, 07 März 2009 21:55 ] [ ID #1992572 ]

Re: Problem with PDO exceptions

--------------040001050001010400030702
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Hi,

Did you set the value of the |PDO::ATTR_ERRMODE| attribute to
|PDO::ERRMODE_EXCEPTION|?

Btw, I don't think ||prepare would throw an exception even for a
malformed query, but ||execute definitely should.

Regards,
Z


--------------040001050001010400030702--
Zoltan Ormandi [ So, 08 März 2009 07:35 ] [ ID #1992651 ]

Re: Problem with PDO exceptions

--001517503d86f022d50464961b62
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

yup, I have set the |PDO::ATTR_ERRMODE| attribute to
|PDO::ERRMODE_EXCEPTION|
and I am still not getting an exception!

My Code is something like this:
$query = $db->prepare($sql);
$query->execute($bind);
$row = $query->fetch(PDO::FETCH_ASSOC);

wher $db is the PDO obj and $sql is the sql query and $bind is the bound
parameter

On Sun, Mar 8, 2009 at 5:35 PM, Zoltan Ormandi <ormandi.zoltan [at] webfunteam.hu>
wrote:
> Hi,
>
> Did you set the value of the |PDO::ATTR_ERRMODE| attribute to
> |PDO::ERRMODE_EXCEPTION|?
>
> Btw, I don't think ||prepare would throw an exception even for a malformed
> query, but ||execute definitely should.
>
> Regards,
> Z
>
>

--001517503d86f022d50464961b62--
Kesavan Rengarajan [ So, 08 März 2009 08:03 ] [ ID #1992652 ]

Re: Problem with PDO exceptions

--000e0cd16e801b41420464964412
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

I am sorry, setting |PDO::ATTR_ERRMODE| attribute to
|PDO::ERRMODE_EXCEPTION| *does* throw an Exception when the table cannot be
found. Stupid me; I was trying to catch Exception rather than a
PDOException.

On Sun, Mar 8, 2009 at 6:03 PM, kesavan trichy rengarajan <k7 [at] trk7.com>wrote:

> yup, I have set the |PDO::ATTR_ERRMODE| attribute to
> |PDO::ERRMODE_EXCEPTION|
> and I am still not getting an exception!
>
> My Code is something like this:
> $query = $db->prepare($sql);
> $query->execute($bind);
> $row = $query->fetch(PDO::FETCH_ASSOC);
>
> wher $db is the PDO obj and $sql is the sql query and $bind is the bound
> parameter
>
>
> On Sun, Mar 8, 2009 at 5:35 PM, Zoltan Ormandi <
> ormandi.zoltan [at] webfunteam.hu> wrote:
> > Hi,
> >
> > Did you set the value of the |PDO::ATTR_ERRMODE| attribute to
> > |PDO::ERRMODE_EXCEPTION|?
> >
> > Btw, I don't think ||prepare would throw an exception even for a
> malformed
> > query, but ||execute definitely should.
> >
> > Regards,
> > Z
> >
> >
>
>

--000e0cd16e801b41420464964412--
Kesavan Rengarajan [ So, 08 März 2009 08:14 ] [ ID #1992653 ]

Re : Problem with PDO exceptions

>Message-ID: <49B2CE8F.1090500 [at] theingots.org>
>Date: Sat, 07 Mar 2009 20:44:15 +0100
>From: Daniel Carrera <daniel.carrera [at] theingots.org>
>
>Hello,
>
>I have MySQL 5.1 and PHP 5.2. For some reason PDO is not throwing
>exceptions when I give it a broken SQL query. For example:
>
>try {
> $stmt = $db->prepare("SELECT * FROM foobar WHERE 1");
>} catch(PDOException $e) {
> error($e->getMessage());
>}
>
>In this example there is no table called 'foobar', so this should
>give an error. Yet, it doesn't.


When you create your DB connection $db, follow the connection line
directly after with this :
$db->setAttribute(PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION);

The default is I believe PDO::ERRMODE_SILENT which is confusing to
most people the first time.
http://uk2.php.net/manual/en/pdo.setattribute.php


HTH
Cheers - Neil



--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Neil Smth [ Mo, 09 März 2009 01:10 ] [ ID #1992754 ]

Re: Re : Problem with PDO exceptions

Neil Smith [MVP, Digital media] wrote:
> When you create your DB connection $db, follow the connection line
> directly after with this :
> $db->setAttribute(PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION);
>
> The default is I believe PDO::ERRMODE_SILENT which is confusing to most
> people the first time.


Thanks! It works now.

Cheers,
Daniel.

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Daniel Carrera [ Mo, 09 März 2009 11:54 ] [ ID #1992755 ]
PHP » gmane.comp.php.database » Problem with PDO exceptions

Vorheriges Thema: MySQL + PHP on WinXP x64 = ??
Nächstes Thema: Retrieving Image Location in MySQL