Skip to content

SQLSTATE[42000]: Syntax error or access violation: 1064

2009 October 5
by Richard Knop

If you are using PDO adapter to work with databases in Zend Framework you might come across an exception similar to this:

Message: SQLSTATE[42000]: Syntax error or access violation: 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 ')' at line 2

It took me some time to track down the cause of this error message. I was building a complex query with Zend_Db_Select which included several joins and subqueries so I thought I had made a mistake in syntax somewhere but the problem was I was passing an undefined variable to where() method. So the problematic line was:

$select->where($where);

And $where was not defined. Hope this helps somebody.

Addendum (Oct 6, 2009 @ 14:13)

The same problem can occur also when there is any error in your WHERE clause, for instance:

WHERE id =

The above (there’s a missing integer after “id =”) would throw the same exception.

4 Responses leave one →
  1. October 13, 2009

    Thanks, it did help a bit. My error was very very annoying, see if you can’t spot it:

    ->getAdapter()->quoteInto(‘ id => ?’, 123)

  2. October 13, 2009

    id => ?

    should be

    id = ?

  3. July 10, 2010

    Thanks man, this was very helpful!!!

  4. Jason permalink
    September 8, 2011

    This can also happen when you used a reserved word in your SQL statement… (It happened to me earlier with “read”

    http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS