CodeIgniter - ORDER BY on a query

2019-01-16 18:21发布

问题:

I have a very small script to get all records from a database table, the code is below

$query = $this->db->get($this->table_name);
return $query->result();

using this syntax, how would i ORDER BY 'name' ?

I get errors everytime i stick the order by bit on the end.

Cheers,

回答1:

I believe the get() function immediately runs the selection query and does not accept ORDER BY conditions as parameters. I think you'll need to separately declare the conditions, then run the query. Give this a try.

$this->db->from($this->table_name);
$this->db->order_by("name", "asc");
$query = $this->db->get(); 
return $query->result();


回答2:

Using this code to multiple order by in single query.

$this->db->from($this->table_name);
$this->db->order_by("column1 asc,column2 desc");
$query = $this->db->get(); 
return $query->result();


回答3:

Simple and easy:

$this->db->order_by("name", "asc");
$query = $this->db->get($this->table_name);
return $query->result();


回答4:

Just add the'order_by' clause to your code and modify it to look just like the one below.

$this->db->order_by('name', 'asc');
$this->db->where('table_name');

There you go.



回答5:

Try This:

        $this->db->select('main.*');
        $this->db->from("ci_table main");
        $this->db->order_by("main.id", "DESC");
        return $this->db->get()->result();


回答6:

100% Working!!!!

$this->db->order_by('price', 'ASC');
$q=$this->db->get('add_new_car');
return $q->result_array();


回答7:

Use order_by:

$this->db->order_by("coloumn_name", "desc");
$query = $this->db->get('table_name');
return $query->result();


标签: codeigniter