Kedar B Kedar B - 13 days ago 6
MySQL Question

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

This is my code in model..

function get_info($product_id)
{
$this->db->from('product');
$this->db->where('product_id',$product_id);

$query = $this->db->get();

if($query->num_rows()==1)
{
return $query->row();
}


So many questions on stackoverflow but I Dont found any answer related to scenario. So what I am doing wrong??

Answer

Your query is probably failing. Make sure database debugging in on and modify your code:

if ($query !== FALSE)
{
    // Run your code
    if ($query->num_rows() === 1)
    {
        return $query->row();
    }
}
else
{
    // Check error
    echo 'Database Error(' . $this->db->_error_number() . ') - ' . $this->db->_error_message();
}