如何使用CodeIgniter按降序对数据库值进行排序?

时间:2014-05-18 07:06:37

标签: php mysql codeigniter

我对CI很新。我想在我的模型中使用下面的查询按降序对数据库值进行排序。但是,它不适用于我并抛出错误。请帮助我。

   function get_records(){      
    $this->db->from($this->tbl_contactus);
    $this->db->order_by("contactus_id", "asc");
    $query = $this->db->get();
    return $query->result();
}

错误: enter image description here

2 个答案:

答案 0 :(得分:2)

试试这个

   function get_records(){

    $this->db->select("*");
    $this->db->from("tbl_contactus");
    $this->db->order_by("contactus_id", "desc");
    $query = $this->db->get();
    return $query->result();

}

也可以使用此

function get_records(){

    $this->db->select()->from('tbl_contactus')->order_by('contactus_id', 'desc');

}

答案 1 :(得分:1)

您必须检查$tbl_contactus,因为它未在该行中定义或为空

  $this->db->from($this->tbl_contactus);

你正在使用这样的查询:

    SELECT * ORDER BY contactus_id ASC

虽然它应该是

    SELECT * FROM your_table ORDER BY contactus_id ASC