By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,946 Members | 1,676 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,946 IT Pros & Developers. It's quick & easy.

How to know if there is a mysql syntax error from pdo

guillermobytes
P: 77
Hi,
i'm making a query with PDO and there is a SQL syntax error in it.

Expand|Select|Wrap|Line Numbers
  1. $sql = 'BAD CODE';
  2. $pdoStmt = $pdo->prepare($sql);
  3. if (false === $pdoStmt) {
  4.     echo 'ERROR';
  5. }
it does not echo 'ERROR'

any suggestions on how i should test a syntax error?
Mar 16 '10 #1
Share this Question
Share on Google+
11 Replies


Dormilich
Expert Mod 5K+
P: 8,639
if I remember right, PDO throws a PDOException in this case …
Return Values

If the database server successfully prepares the statement, PDO::prepare() returns a PDOStatement object. If the database server cannot successfully prepare the statement, PDO::prepare() returns FALSE or emits PDOException (depending on error handling).
Mar 16 '10 #2

guillermobytes
P: 77
ok, wierd, because it didn't throw any Exception neither :S
Mar 16 '10 #3

Dormilich
Expert Mod 5K+
P: 8,639
I suspect, "BAD CODE" isn’t enough to cause a MySQL error …
Mar 16 '10 #4

Atli
Expert 5K+
P: 5,058
See PDO::errorInfo.

@Dormilich
It does.
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BAD CODE' at line 1
Mar 17 '10 #5

Atli
Expert 5K+
P: 5,058
That's odd... The example in the manual, using the PDO::prepare method doesn't work, but if you substitute it for PDO::query, then it does...

Expand|Select|Wrap|Line Numbers
  1. $stmt = $dbh->query('bogus sql');
  2. if (!$stmt) {
  3.     echo "\nPDO::errorInfo():\n";
  4.     print_r($dbh->errorInfo());
  5. }
Expand|Select|Wrap|Line Numbers
  1. PDO::errorInfo():
  2. Array
  3. (
  4.     [0] => 42000
  5.     [1] => 1064
  6.     [2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'bogus sql' at line 1
  7. )
Mar 17 '10 #6

Atli
Expert 5K+
P: 5,058
Ahh Ok, you need to execute it and use the PDOStatement::errorInfo field.
Expand|Select|Wrap|Line Numbers
  1. $stmt = $dbh->prepare('bogus sql');
  2. $res = $stmt->execute();
  3. if (!$res) {
  4.     echo "\nPDO::errorInfo():\n";
  5.     print_r($stmt->errorInfo());
  6. }
... Not often that you see errors like that in the manual :|
Mar 17 '10 #7

Markus
Expert 5K+
P: 6,050
What error?
Mar 17 '10 #8

Atli
Expert 5K+
P: 5,058
@Markus
According to example #1 for PDO::errorInfo, this:
Expand|Select|Wrap|Line Numbers
  1. $stmt = $dbh->prepare('bogus sql');
  2. if (!$stmt) {
  3.     echo "\nPDO::errorInfo():\n";
  4.     print_r($dbh->errorInfo());
  5. }
Should produce something like:
Expand|Select|Wrap|Line Numbers
  1. PDO::errorInfo():
  2. Array
  3. (
  4.     [0] => HY000
  5.     [1] => 1
  6.     [2] => near "bogus": syntax error
  7. )
However, according to my tests, it does not.
Granted, I tested this against a MySQL database rather than DB2, but PDO should act consistently despite which database you are using.
Mar 17 '10 #9

Markus
Expert 5K+
P: 6,050
Oooh. I'll update the docs.
Mar 17 '10 #10

guillermobytes
P: 77
ok thanks for the details
Mar 17 '10 #11

Atli
Expert 5K+
P: 5,058
@Markus
You can do that? :O
Nice :D
Mar 17 '10 #12

Post your reply

Sign in to post your reply or Sign up for a free account.