Call to a member function num_rows() on a non-object

Posted by Patrick on Stack Overflow See other posts from Stack Overflow or by Patrick
Published on 2010-04-03T20:23:43Z Indexed on 2010/04/03 20:33 UTC
Read the original article Hit count: 313

Filed under:
|

I need to get the number of rows of a query (so I can paginate results).

As I'm learning codeigniter (and OO php) I wanted to try and chain a ->num_rows() to the query, but it doesn't work:

//this works:
$data['count'] = count($this->events->findEvents($data['date'], $data['keyword']));

//the following doesn't work and generates
// Fatal Error: Call to a member function num_rows() on a non-object
$data['count2'] = $this->events->findEvents($data['date'], $data['keyword'])->num_rows();

the model returns an array of objects, and I think this is the reason why I can't use a method on it.

function findEvents($date, $keyword, $limit = NULL, $offset = NULL)
{
    $data = array();

    $this->db->select('events.*, venues.*, events.venue AS venue_id');
    $this->db->join('venues', 'events.venue = venues.id');

    if ($date)
    {
        $this->db->where('date', $date);
    }

    if ($keyword)
    {
        $this->db->like('events.description', $keyword);
        $this->db->or_like('venues.description', $keyword);
        $this->db->or_like('band', $keyword);
        $this->db->or_like('venues.venue', $keyword);
        $this->db->or_like('genre', $keyword);
    }

    $this->db->order_by('date', 'DESC');
    $this->db->order_by('events.priority', 'DESC'); 
    $this->db->limit($limit, $offset); //for pagination purposes
    $Q = $this->db->get('events');
    if ($Q->num_rows() > 0)
    {
        foreach ($Q->result() as $row)
        {
            $data[] = $row;
        }
    }

    $Q->free_result();
    return $data;
}   

Is there anything that i can do to be able to use it? EG, instead of $data[] = $row; I should use another (OO) syntax?

© Stack Overflow or respective owner

Related posts about codeigniter

Related posts about oop