Sourabh Kumar
Sourabh Kumar

Reputation: 94

count the not null value using where condition in codeigniter

I am writing a function to count the null column with where condition but there is a problem in this function

protected function _get_mcq_attept_count2( $mcq_id){
    $this->load->model('museranswer');  
    return $this->museranswe>count_by(array('mcq_id'=>$mcq_id,'bookrefrence!='=>" "));
}

this function made the query

SELECT COUNT(*) AS `numrows`
FROM `user_answer`
WHERE `mcq_id` = '321'
AND `bookrefrence` != ' '

this query return the empty column value

Upvotes: 1

Views: 2700

Answers (3)

Sourabh Kumar
Sourabh Kumar

Reputation: 94

return $this->museranswer->count_by(array('mcq_id'=>$mcq_id,'length(bookrefrence)>2'));

Upvotes: 0

user6161070
user6161070

Reputation:

I hope this code work for it bcz in code-igniter i always use like this .

 protected function _get_mcq_attept_count2($mcq_id)
    {
        $this->load->model('museranswer');  
        $where = array('mcq_id'=>$mcq_id);
        return $this->museranswe>count_by($where);
    }

    /******************* FOR MODEL *********************/

    public function count_by($where)
    {
        $this->db->select('count(mcq_id) as numrows');
        $this->db->from('user_answer');
        $this->db->where($where);
        $this->db->where('bookrefrence !=',' ');

        $qry = $this->db->get();
        return $qry->result_array();
    }

Upvotes: 1

Jobayer
Jobayer

Reputation: 1231

Change your query like this array("mcq_id" => "$mcq_id", "bookrefr‌​ence IS NOT NULL" => null). Hope you will get right answer. If it does not work, share your model with us.

Upvotes: 0

Related Questions