php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40772 PDO::query() does not raise exception
Submitted: 2007-03-10 18:49 UTC Modified: 2007-03-25 12:23 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: vunnuk_o at ukr dot net Assigned:
Status: Closed Package: PDO related
PHP Version: 5.2.1 OS: Win XP
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: vunnuk_o at ukr dot net
New email:
PHP Version: OS:

 

 [2007-03-10 18:49 UTC] vunnuk_o at ukr dot net
Description:
------------
PDO::query() does not throw an error when the SQL query contains syntax error

Reproduce code:
---------------
$pdo = new PDO($dsn, $user, $pass, array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION));
$pdo->query("SELETC * FROM test");

Expected result:
----------------
Exception should be raised.


Actual result:
--------------
Exception is not raised
but $pdo->errorCode() reports 42000

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-03-11 14:42 UTC] iliaa@php.net
What PDO back-end are you using?
 [2007-03-11 15:47 UTC] vunnuk_o at ukr dot net
This happens for MySQL an SQLite
 [2007-03-11 15:53 UTC] vunnuk_o at ukr dot net
However, SQLite's errorCode() is HY000... should not be 42000?
 [2007-03-11 17:24 UTC] dennis at inmarket dot lviv dot ua
Looks like PDO constructor does not like the PDO::ATTR_ERRMODE in the options array. This way works:

$conn = new PDO($dsn, $user, $pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->query("SELETC * FROM test"); // throws a PDOException

However, for SQLite the SQLSTATE still is HY000 instead of 42000
 [2007-03-11 18:31 UTC] iliaa@php.net
Unfortunately on syntax errors SQLite returns a generic "error" message, 
which is why PHP returns HY000 error code.

As far as constructor not support exception option setting, I cannot 
seem to replicate in SQLite. Here is the sample code I am using


$d = new PDO('sqlite:test.db', NULL, NULL, array(PDO::ATTR_ERRMODE, 
PDO::ERRMODE_EXCEPTION));
$d->query("SELETC * FROM test");
 [2007-03-11 20:02 UTC] vunnuk_o at ukr dot net
This works on Linux (SQLite) but fails on Windows. So still a bug. Also, your example has an error

array(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION));
should be 
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));

Otherwise on Linux it leads to another error:
The auto-commit mode cannot be changed for this driver
 [2007-03-12 09:22 UTC] tony2001@php.net
>This works on Linux (SQLite) but fails on Windows.
Which PHP version are you using?

>Otherwise on Linux it leads to another error:
>The auto-commit mode cannot be changed for this driver
How is that related to your problem and how to reproduce it?
 [2007-03-12 10:59 UTC] vunnuk_o at ukr dot net
>>This works on Linux (SQLite) but fails on Windows.
>Which PHP version are you using?
5.2.1

>>Otherwise on Linux it leads to another error:
>>The auto-commit mode cannot be changed for this driver
>How is that related to your problem and how to reproduce it?
It's not related; just telling that Ilia's code has an error:

$d = new PDO('sqlite:test.db', NULL, NULL, array(PDO::ATTR_ERRMODE, 
PDO::ERRMODE_EXCEPTION));

should be 

$d = new PDO('sqlite:test.db', NULL, NULL, array(PDO::ATTR_ERRMODE => 
PDO::ERRMODE_EXCEPTION));
 [2007-03-12 11:12 UTC] tony2001@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5.2-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5.2-win32-latest.zip


 [2007-03-20 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2007-03-25 12:23 UTC] vunnuk_o at ukr dot net
With the latest snapshot this error does not manifest itself
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 11:01:33 2024 UTC