How to fix: Drupal “user warning: Unknown column…” error

Exact error message:
...
user warning: Unknown column 'page' in 'where clause' query: [function_name] /* [username] : [function_name] */ SELECT n.nid FROM node n WHERE n.type = page AND n.status = 1 ORDER BY n.created DESC LIMIT 0, 5 in /home/user/public_html/path/to.module on line 294.
...

Th query in question:
$result = db_query('SELECT n.nid FROM {node} n WHERE n.type = %s AND n.status = 1 ORDER BY n.created DESC LIMIT 0, %d', variable_get('some_var', NULL), variable_get('another_var', 5));

In my case, it was missing quotes for %s. See below.
$result = db_query('SELECT n.nid FROM {node} n WHERE n.type = "%s" AND n.status = 1 ORDER BY n.created DESC LIMIT 0, %d', variable_get('some_var', NULL), variable_get('another_var', 5));

Drupal developer? Get pro with Pro Drupal Development, Second Edition (Volume 0)

No Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: