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)

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.

If you enjoyed this post, make sure you subscribe to our RSS Feed! Or if you prefer, you can Follow us on Twitter instead.

This is my notes page -- a scratch pad. Sometimes I do not write them correctly. I wrote here so I can access my notes from everywhere. If you want to use them, use with caution. Please check and re-check the syntax. You've been warned!