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.

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

1 Answer 1

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

0

Your Answer

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

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