473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

guillermobytes
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
11 2630
Dormilich
8,658 Expert Mod 8TB
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
ok, wierd, because it didn't throw any Exception neither :S
Mar 16 '10 #3
Dormilich
8,658 Expert Mod 8TB
I suspect, "BAD CODE" isn’t enough to cause a MySQL error …
Mar 16 '10 #4
Atli
5,058 Expert 4TB
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
5,058 Expert 4TB
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
5,058 Expert 4TB
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
6,050 Expert 4TB
What error?
Mar 17 '10 #8
Atli
5,058 Expert 4TB
@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
6,050 Expert 4TB
Oooh. I'll update the docs.
Mar 17 '10 #10
ok thanks for the details
Mar 17 '10 #11
Atli
5,058 Expert 4TB
@Markus
You can do that? :O
Nice :D
Mar 17 '10 #12

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

Similar topics

7
by: Julien - Marseille | last post by:
Hello, I need help for php syntax when i call Mysql database I have wrote that and my sql connection is working I just have a problem with this command line : $query = "SELECT * FROM...
23
by: phpfrizzle | last post by:
Hi there, I have a site with products on it. The site has a mysql backend. All products belong to certain series (table series). There can be up to 4 different products (table products)...
0
by: Morten Gulbrandsen | last post by:
Hi programmers, I try to investigate some of the basics behind schemas and cataloges, Which is part of SQL2 Language this is the error message I get:
0
by: Fatt Shin | last post by:
Hi, I'm running MySQL 4.0.13, connecting from PowerBuilder 9 using ODCB Connector 3.51. I'm facing a problem where whenever I issue a SELECT COUNT(*) statement from PowerBuilder, I always get SQL...
0
by: Mario Ohnewald | last post by:
Hello! I want to import a MySQL 3.x Databse to my new shiny MySQL4.x. I did a backup with /usr/local/mysql-standard-4.0.14-pc-linux-i686/bin/mysqldump -u root -p --opt -A > mysql_lamp_backup.`date...
4
by: James E Koehler | last post by:
I can't get the WHILE statement to work in MySQL. The version of MySQL that I am using is: Ver 12.16 Distrib 4.0.6-gamma, for Win95/Win98 (i32) running on Windows MX. Here is the relevant...
13
by: wideangle | last post by:
Hello there! I know it's stupid, but when creating a table in a mysql (win32) database, it won't let me create this "mytable". Here goes my ER_PARSE_ERROR. mysql> CREATE TABLE `mytable` ( ->...
2
by: speralta | last post by:
My tired old eyes may be failing me, but the following insert statements look correct to me, but I can't seem to get a clean insert from a fairly large text file database into mysql. I was...
2
josie23
by: josie23 | last post by:
Egad, I'm not a coder/programmer by nature or occupation but understand things like html and css and a small amount of perl. So, basically, I'm a perl/mysql imbecile. But, I've been trying to...
5
by: gsaray101 | last post by:
I am trying to set up one server with multiple mysql dbs. This script will get the mysqldump files from the ftp server and based on the name of the server in the file, it will create the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.