php CodeIgniter get_where order_by
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13325962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CodeIgniter get_where order_by
提问by user979331
I am tring to use get_where and order_by like so...
我想像这样使用 get_where 和 order_by ......
$query = $this->db->get_where($this->tbl_name, $where)->order_by('birth_date', 'ASC');
but got this error...
但得到这个错误...
Fatal error: Call to undefined method CI_DB_mysql_result::order_by() in C:\xampp\htdocs\OAWA\application\models\Member_model.php on line 82
What Am i doing wrong?
我究竟做错了什么?
回答by xbonez
In CodeIgniter's Active Record, every method returns the object itself (which allows method chaining) except for getand get_wherewhich return the result set.
在 CodeIgniter 的 Active Record 中,除了返回结果集的get和之外,每个方法都返回对象本身(允许方法链接)get_where。
Thus, what you need to do is:
因此,您需要做的是:
$query = $this->db->order_by('birth_date', 'ASC')->get_where($this->tbl_name, $where);
i.e. The get_where()call needs to be the last one. It returns the result set, and so calling order_by()after get_where()is attempting to call it on the result set which is invalid.
即get_where()呼叫需要是最后一个。它返回结果集,因此调用order_by()afterget_where()试图在无效的结果集上调用它。
EDIT
编辑
There are other ways to write this query as well:
还有其他方法可以编写此查询:
$query = $this->db->from($this->tbl_name)->where($where)->order_by('birth_date', 'ASC')->get();
回答by Laurence Meijer
This worked for me
这对我有用
$query = $this->db->order_by('columnName', 'ASC')->get_where('tableName');
return $query->result();
回答by Tushar Madaan
$this->db->order_by('birth_date', 'ASC');
$query = $this->db->get_where($field1, $field2);
回答by lazyme114
$mysql_query = "select * from table where col_name='$where' order by birth_date asc";
$mysql_query = "select * from table where col_name='$where' order bybirth_date asc";
$query = $this->db->query($mysql_query);
$query = $this->db->query($mysql_query);
I tried this just now and it works for me
我刚刚试过这个,它对我有用

