0

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..

10
  • Syntax error or access violation Check you have your connection string correct. This could be caused by a wrong username/password. Commented Mar 14, 2012 at 16:14
  • @vascowhite got my connection working, thats not the problem. :\ Commented Mar 14, 2012 at 16:17
  • try $query->where("value = '?', $number);. I can't see anything else that could be wrong. Commented Mar 14, 2012 at 16:22
  • do you have a mighty_table.value column in your db? Commented Mar 14, 2012 at 16:23
  • If you run the query in your database directly does it work? Commented Mar 14, 2012 at 16:42

2 Answers 2

3

From zend manual :

Note: The values and identifiers in the SQL expression are not quoted for you. If you have values or identifiers that require quoting, you are responsible for doing this. Use the quote(), quoteInto(), and quoteIdentifier() methods of the database adapter.

So for example you can use quoteInto :

$number = 4;
$query = $this->select();
$where = $this->getAdapter()->quoteInto('value = ?', $number);
$query->where($where);
$row = $this->fetchRow($query);
Sign up to request clarification or add additional context in comments.

2 Comments

This one will probably need to be ->quoteIdentifier(), I'm pretty sure that VALUE is a keyword in Mysql, I know that VALUES is.
That's in fact the reason why my code was all buggy. Thanks for the answer! :)
1

"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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.