Search code examples
mysqlzend-frameworkzend-db

Zend DB where clause error


EDIT:

$query->where('`value` = ?', $number); It seems that does the job. I still don't know why it won't work under normal conditions, but it's a work around.. still looking for the right answer!


I'm trying to query a DB with a simple:

$number = 4;
$query = $this->select();
$query->where('value = ?', $number);
$row = $this->fetchRow($query);

But for some reason I constantly got this error:

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 'value = 4) LIMIT 1' at line 1

When I do my assemble to see the query string:

SELECT `mighty_table`.* FROM `mighty_table` WHERE (value = 4)

My column name its not escaped..

Should Zend DB do that? :| It's strange since i use this same method in other projects and it always works..


Solution

  • "value" is indeed a reserved word in MySQL. As a result you need to escape it using back ticks.

    I would expect this to work:

    $fieldName = $this->getAdapter()->quoteIdentifier('value');
    $query->where($fieldName = ?", $number);