CI - num_rows not returning a readable number

Using CodeIgniter 3 and num_rows() does not return a readable number (it's empty) using return. The only way to make it show the actual number is if I use print or echo - but that breaks the functionality I need the function to have.

    public function count_notifications($user_id)
    {
        $query = $this->db->select("*")
            ->where("user_id", $user_id)
            ->order_by("time_received", "desc")
            ->get("members_notifications")
            ->num_rows();

        return $query;
    }

Why is it behaving like that?

4 answers

  • answered 2018-02-13 02:34 Alex

    num_rows() will always return some integer whether it be int(0) for no results or int(4). I ran my own test that was very similar to yours because for a second I thought it might be possible (it wasn't).

    You should go through the typical debugging routes. The only thing I can think of is that there is an error in your syntax or wording that is resulting in get() returning boolean (and maybe you don't see that as you have error reporting off).

    I'm not sure how you are verifying that the function isn't returning an integer but perhaps start there as your logic there might be the problem.

  • answered 2018-02-13 02:34 DFriend

    Give this a try.

    public function count_notifications($user_id)
    {
        $query = $this->db->where("user_id", $user_id)
                 ->get("members_notifications");            
        return $query->num_rows();
    }
    

    I really wouldn't expect it to be different than your code. But you can easily confirm whether $query is a CI_DB_result or a bool.

  • answered 2018-02-13 02:34 Abdulla Nilam

    Methods you can do

    1. Can use both Query Builder and Query Result to get data
    2. Can use count_all_results which assicated with Query Builder

    Usage

    Note:

    1. Reason why I remove this ->order_by("time_received", "desc") is, order_by won't help when I'm just counting rows. Because It's int value.
    2. If you need to see the output and stop executing after this. you can/need to add die(); after the echo statements. (ex: echo $query; die();)

    Method 01

    public function count_notifications($user_id)
    {
        $query = $this->db->select("*")
                 ->where("user_id", $user_id)
                 ->get("members_notifications");
        return $query->num_rows(); #holds int; ex 5
    }
    

    Method 02

    public function count_notifications($user_id)
    {
        $query = $this->db->select("*")
                 ->where("user_id", $user_id)
                 ->get("members_notifications")
                 ->count_all_results(); # holds int; ex 5
        return $query;
    }
    

    In Controller

    $count = $this->model_name->count_notifications($user_id);
    echo $count; # to show
    $data['count'] = $count; # pass to view
    

  • answered 2018-02-13 02:34 Apache

    Try using the following way:

    public function count_notifications($user_id)
    {
         $query = $this->db->select("*")
                ->where("user_id", $user_id)
                ->order_by("time_received", "desc")
                ->get("members_notifications");
    
        return $query->num_rows();
    }