0

I am bit new to CI.I want to sort my database values in descending order using this query below in my model. However, it doesn't work form me and throw errors .please help me out.

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

The error: enter image description here

3
  • Your property $this->tbl_contactus is empty according to error Commented May 18, 2014 at 7:08
  • that is impossible it works with //$query = $this->db->get('tbl_contactus'); //return $query->result(); Commented May 18, 2014 at 7:11
  • If you are sure that your property is not empty then add $this->db->select('*') before from function's line it is the correct format to write query with active record Commented May 18, 2014 at 7:14

2 Answers 2

3

Try like this

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

}

and also you can use this

function get_records(){

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

}
Sign up to request clarification or add additional context in comments.

3 Comments

You can directly use $this->db->get($tbl_contactus)->order_by('contactus_id',desc)->result();
$this->db->select()->from('tbl_contactus')->order_by('contactus_id', 'desc'); is also correct
->select() and ->select('*') are never necessary in CodeIgniter -- SELECT * is the default clause anyhow.
1

you have to check $tbl_contactus because its not defined or empty in that line

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

You are using query like that:

    SELECT * ORDER BY contactus_id ASC

while it should be

    SELECT * FROM your_table ORDER BY contactus_id ASC

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.