Search code examples
mysqldrupaldrupal-7

SQL in Drupal. WHERE AND


The following works...

global $user;
$items = array();
$sql = 'SELECT nid FROM {node} WHERE uid = :uid';
$result = db_query($sql, array(':uid' => $user->uid));
foreach ($result as $row) {
  $items[] = $row->nid;
}
dsm($items);

However, when I want to select the content type "venue" from the "type" column in the same database tables, I get errors using the following...

global $user;
$items = array();
$sql = 'SELECT nid FROM {node} WHERE uid = :uid AND type = venue';
$result = db_query($sql, array(':uid' => $user->uid));
foreach ($result as $row) {
  $items[] = $row->nid;
}
dsm($items);

DOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'venue' in 'where clause': SELECT nid FROM {node} WHERE uid = :uid AND type = venue; Array ( [:uid] => 1 )

Im ovbiously not understanding something here. The column is called "type", im not asking it to look for a column called "venue" am i?


Solution

  • When comparing a column to a string you need to wrap the string with quotes, if you dont, the optimizer will see this as a column(unless its a number) . Try this:

    $sql = "SELECT nid FROM {node} WHERE uid = :uid AND type = 'venue'"