调用模型文件中非对象上的成员函数 num_rows()


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

这是我在模型中的代码。

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();
    }

关于堆栈溢出的问题很多,但我没有找到任何与场景相关的答案。那我做错了什么??

您的查询可能失败。确保数据库调试并修改代码:

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();
}

结果似乎您什么也没找到,因此查询返回 false。另一个选项是数据库连接问题,请在配置文件中检查它。

你能执行任何SQL查询吗?我怀疑您的数据库连接根本不起作用...检查数据库设置