0

Can't figure out why the following query executes directly through phpMyAdmin perfectly well:

SELECT id FROM `stats` WHERE type="load" ORDER BY id DESC LIMIT 1

but firing this:

db_query('SELECT id FROM {stats} WHERE type = '.$param.' ORDER BY id DESC LIMIT 1')->fetchField();

from a module outputs:

PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'load' in 'where clause': SELECT id FROM {server_stats} WHERE type = load5 ORDER BY id DESC LIMIT 1; Array ( ) in server_tab_stats_submit() (line 750 of /home/dev/public_html/sites/all/modules/custom/server/server.inc).

Column's name there is "type", I don't know why Drupal query thinks "load" is a column, it is just a value derived from $param variable.

  • Anil has already answered the right way but .. you need quotes also: type = "'.$param.'" ORDER BY – AgA Sep 18 '14 at 5:21
  • Tried this way, unfortunately didn't work. But I am happy with Anil's solution. Thanks! – Sasha Sep 18 '14 at 5:32
  • db_select gives you less freedom to err ;) – Mołot Sep 18 '14 at 8:41
0

Try This

$nid = db_query('SELECT id FROM {stats} WHERE type = :type ORDER BY id DESC LIMIT 1', array(':type' => $param))->fetchField();

Refer db_query for more information...

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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