|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 29 16:00:02 2025 UTC |
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 42000Unfortunately 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");>>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));