I have this snippet:

$nid = db_query("select nid from {node} where title = :title", array(':title' = 'scrivimi'))->fetchField();

It should work, at least from what I've read around the drupal api reference, but it doesn't.

I think the problem is with placeholders. In fact, if I execute the query without them, it runs perfectly, and returns the expected result!

Can you help me please?

I am using Drupal 7 (updated to the latest version), and I am testing this code using the Devel module.

  • kiamlaluno, clive: thank you very much for your replies! I wasn't thinking at all that the "=" instead of "=>" could be the problem!!! and (for clive), yes, I know that SQL Statements should be written uppercase; but, unfortunately I was in a hurry while posting the code and I forgot to write them uppercase instead of lowercase! – Vincenzo Oct 14 '12 at 14:16

There is a typo in your code: In the array literal, you are using = instead of => which should separate the array index from its value.

The correct code is the following one.

$nid = db_query("SELECT nid FROM {node} WHERE title = :title", array(':title' => 'scrivimi'))->fetchField();

As side note, every SQL reserved words should be written in uppercase.


You have a tiny error in your code, the placeholder array should be

array(':title' => 'scrivimi')


array(':title' = 'scrivimi')

Not the answer you're looking for? Browse other questions tagged or ask your own question.