How many lines did a database query return (Cake PHP)?

Asked

Viewed 685 times

1

$this->Post->Comment->find
    ('all', array('conditions' => array('Comment.post_id' => $id)));

I do this query, how to know if returned 5, 7, 10... comments before displaying them in the view?

2 answers

3


I suppose you’re holding the result in a variable, right? Something like that:

$comentarios = $this->Post->Comment->find('all', array('conditions' => array('Comment.post_id' => $id)));

In this case, use the function count php to get the amount:

$quantidade = count($comentarios);
  • That’s right. Thanks man!

1

Use the find('count');

$count = $this->Post->Comment->find
    ('count', array('conditions' => array('Comment.post_id' => $id)));
  • Thanks! But in the precise case of the query data also!

  • @Iwannaknow soh to leave archived, Voce can use both 'Count' and 'all''

  • At the same time? find('Count', 'all'... ?

  • @Iwannaknow one at a time hehe

  • So I do the query and take advantage of it using 'Count' as @bfaveretto put it. But thanks again.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.