3

I have this snippet:

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

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.

closed as too localized by kiamlaluno Oct 28 '12 at 19:48

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 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
0

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.

3

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

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

not

array(':title' = 'scrivimi')

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