0

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')
 ->fields('cid')
  ->condition('nid', $node->nid,'=');

  $record=$result->execute()->fetch();

But I get error. So what is my mistake?

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

Hope this helps you.

| improve this answer | |
  • 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. – user3530082 Oct 6 '14 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 '14 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 ? – user3530082 Oct 6 '14 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 – user3530082 Oct 6 '14 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? – user3530082 Oct 6 '14 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.