如何使用CodeIgniter的Active Record方法添加ORDER BY子句? [英] How to add an ORDER BY clause using CodeIgniter's Active Record methods?

查看:74
本文介绍了如何使用CodeIgniter的Active Record方法添加ORDER BY子句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常小的脚本,可以从数据库表中获取所有记录,代码如下.

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

使用这种语法,如何在我的选择查询中添加ORDER BY 'name'子句?

Using this syntax, how would I add a ORDER BY 'name' clause to my select query?

每次我最后一点点下订单时都会出错.

I get errors every time I stick the order by bit on the end.

推荐答案

我相信get()函数会立即运行select查询,并且不接受ORDER BY条件作为参数.我认为您需要分别声明条件,然后运行查询.试试看:

I believe the get() function immediately runs the select 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();

CodeIgniter文档order_by()

这篇关于如何使用CodeIgniter的Active Record方法添加ORDER BY子句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆