get()在Codeigniter中返回空行

时间:2014-02-17 09:22:19

标签: php codeigniter activerecord get

我正在使用下面的函数,但它没有返回任何行。我尝试$query->num_rows(),但它有null个值。我使用$this->db->last_query()获得了最后一个查询,在数据库中使用了这个查询并且有结果。我错过了Code Igniter中的设置吗?期待你的回复。

function search($conditions=NULL,$tablename="",$orderby='id', $order='DESC',$limit=1000000,$offset=0)
{
        if($tablename=="")
        {
            $tablename = $this->table;
        }
        if($conditions != NULL)
            $this->db->where($conditions);
        if ( $orderby !== "" and $order !== "")
            $this->db->order_by($orderby, $order);
        $query = $this->db->get($tablename,$limit,$offset=0);
    return $query->result_array();
}

2 个答案:

答案 0 :(得分:1)

尝试

if($conditions != NULL)
    $this->db->where($conditions);
if ( $orderby !== "" and $order !== "")
    $this->db->order_by($orderby, $order);
$this->db->limit($limit,$offset);
$query = $this->db->get($tablename);

答案 1 :(得分:0)

首先检查$ tablename是表名还是空白

function search($conditions=NULL,$tablename="",$orderby='id', $order='DESC',$limit=1000000,$offset=0)
    {
            if($tablename=="")
            {
                $tablename = $this->table;
            }
            if($conditions != NULL)
                $this->db->where($conditions);
            if ( $orderby !== "id" and $order !== "DESC")
                $this->db->order_by($orderby, $order);
            $this->db->limit($limit);
            $this->db->offset ($offset );
            $query = $this->db->get($tablename);
       if($query->num_rows() == 0){
    }