I have a {content_type_answers} and this table doesn't has a title row, how can i merge it with {node} to get node titles?

$result = db_query("SELECT {node}.title, {node}.nid FROM {content_type_answers} cta, node WHERE {cta}.nid = {node}.nid");
while($node = db_fetch_object($result) {
    $nodes[] = array($node->title, $node->nid);

Question: how can i select {node}.title from table {content_type_answers} if in this table no title row at all?

  • the title $node->title that you have in your code is the title of the node in content_type_answers. You don't need to have a column title in your content_type_answers table.
    – nourcy
    Dec 20, 2011 at 16:46
  • Have checked my code above? it is correct? after {content_type_answers} cta (it is table name?), node (it is talbe name?) Dec 20, 2011 at 16:58
  • $result = db_query("SELECT node.title, node.nid FROM {content_type_answers} cta, {node} WHERE cta.nid = node.nid"); while($node = db_fetch_object($result) { $nodes[] = array($node->title, $node->nid); }
    – nourcy
    Dec 20, 2011 at 17:01

1 Answer 1

    $q = db_query("
SELECT node.title, node.nid 
FROM content_type_answers cta, node 
WHERE cta.nid = node.nid");
    while ($r = db_fetch_array($q)) {
      $rows[] = $r;
    print theme('table', $header, $rows);
  • This is a alias in order to don't rewrite content_type_answers
    – nourcy
    Dec 20, 2011 at 16:07
  • check my edited question ↑ Dec 20, 2011 at 16:16
  • 1
    You should really use the curly brace syntax in queries to allow Drupal to handle a table prefix. Using proper INNER JOIN syntax rather that the FROM/WHERE variant is also advisable.
    – mpdonadio
    Dec 20, 2011 at 16:27

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.