If you check the comment table, you can see taht each comment has a CID number. For each article I need to have the CID number of all the comments for that article.

So I have written the following in node.tpl.php file:

 $result=db_select('comment', 'c')
  ->condition('nid', $node->nid,'=');


But I get error. So what is my mistake?

1 Answer 1

  $result=db_query('SELECT cid FROM comment WHERE nid = :nid',array(':nid' => $node->nid));

Hope this helps you.

  • So when I write only what you said,I will not get any error, but what should I write to execute the query? when I write $record=$result->execute()->fetch(); it gives me error. Oct 6, 2014 at 4:58
  • There is no need for "$record=$result->execute()->fetch();" put $result in foreach loop and print the result and see.
    – Aparna
    Oct 6, 2014 at 5:13
  • so I wrote foreach($result as $key) echo $key; and I get this error:Recoverable fatal error: Object of class stdClass could not be converted to string in include() (line 187 of C:\xampp\htdocs\myproject\sites\all\themes\Project\templates\node.tpl.php). Do you think I can use this query in node.tpl.php ? Oct 6, 2014 at 5:25
  • also if I write krumo($result), I will see this: ... (Object) DatabaseStatementBase dbh (Object) DatabaseConnection_mysql queryString (String, 40 characters ) SELECT cid FROM comment WHERE Oct 6, 2014 at 5:26
  • and if I write: $result1=$result->execute(); I will get this error: PDOException: SQLSTATE[HY093]: Invalid parameter number: no parameters were bound in include() (line 187 of C:\xampp\htdocs\myproject\sites\all\themes\Project\templates\node.tpl.php).Do you know what is the problem? Oct 6, 2014 at 5:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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