CodeIgniter: Call to undefined method on the result object when using order_by() after get_where()
Call to undefined method CI_DB_mysql_result::order_by()Short answer
get_where() runs the query at once and returns a result object, not the query builder. A result object has no order_by() method, so the call fails. Call order_by() before the query runs: use where(), order_by() and get() in that order, and pass limit and offset to get() or limit().
Fix it fast
1Find out which cause you have
Find the line that chains order_by() after get_where() or get(). Whatever sits left of the arrow is a result, not a builder.
grep -rn -E "get_where\(.*\)->order_by|get\(.*\)->order_by" application/2Match what you see, then jump to its fix
- 1
The failing line looks like ->get_where('table', $where)->order_by(...)order_by() is chained after the query has already runFix - 2
The call is get_where('table', $where, $limit, $offset) and rows must be sortedget_where() was used with a limit, so there seemed no way to sortFix - 3
->getWhere([...])->orderBy(...) in a CodeIgniter 4 model or builderThe same mistake with getWhere() in CodeIgniter 4Fix
- 1
What does the error look like?
Call to undefined method CI_DB_mysql_result::order_by()
Cause 1 of 3
order_by() is chained after the query has already run
1Check
In CodeIgniter 3, get_where() is described as identical to get() with a where condition, and it returns a result object. The guide's every order_by() example comes before get(). The query builder resets after a query runs, so late calls cannot affect it.
// breaks: the query already ran and $query is a result
$query = $this->db->get_where('news', array('status' => 'live'))->order_by('id', 'DESC');2Fix
Build the query first, then run it. Put order_by() before get().
$this->db->where('status', 'live');
$this->db->order_by('id', 'DESC');
$query = $this->db->get('news');
$rows = $query->result();3Verify
Load the page. The rows come back sorted and no fatal error appears.
echo $this->db->last_query();Confirmed by codeigniter.com
Cause 2 of 3
get_where() was used with a limit, so there seemed no way to sort
1Check
get_where() takes the table, the where array, a limit and an offset, and has no place for an order. That is why people chain order_by() after it.
grep -rn "get_where(" application/ | head2Fix
Replace it with where(), order_by() and get() with a limit and offset. get() accepts the limit as its second parameter and the offset as its third.
$this->db->where(array('status' => 'live'));
$this->db->order_by('id', 'DESC');
$query = $this->db->get('news', 10, 20);
// SELECT * FROM news WHERE status = 'live' ORDER BY id DESC LIMIT 20, 103Verify
Print the generated SQL and check the ORDER BY and LIMIT parts.
echo $this->db->last_query();Confirmed by codeigniter.com
Cause 3 of 3
The same mistake with getWhere() in CodeIgniter 4
1Check
In CodeIgniter 4, getWhere() also returns a result object (ResultInterface) after executing the query. The CodeIgniter 4 guide's orderBy() examples run before get().
grep -rn "getWhere(" app/ | head2Fix
Call orderBy() on the builder before get().
$builder = $db->table('news');
$builder->where('status', 'live');
$builder->orderBy('id', 'DESC');
$query = $builder->get();3Verify
Print the SQL the builder would run, keeping its clauses, and check the ORDER BY part.
echo $builder->getCompiledSelect(false);Confirmed by codeigniter4.github.io
How do I stop it coming back?
- Treat get(), get_where() and getWhere() as the end of the chain: build every clause first.
- Prefer where(), order_by(), limit() and get() over get_where() whenever the query needs more than a where.
- Print the last query during development to see the SQL you really sent.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Back end
CodeIgniter 4: try/catch does not catch a failed insert in the model
DatabaseException
CodeIgniter throws a DatabaseException on a failed query only when DBDebug is true, and inside a transaction it does not throw unless you ask.
3 causes, each with a fixBack end
CodeIgniter 4 where() builds a different condition than you meant
A SQL syntax error that appears only when a value contains a quote character
The query builder joins every where() with AND and every orWhere() with OR, with no brackets.
3 causes, each with a fixBack end
Call to undefined function mysql_pconnect() after a PHP upgrade
Call to undefined function mysql_connect()
The old mysql_* functions belonged to the ext/mysql extension, which PHP removed in version 7.0.
2 causes, each with a fixBack end
CodeIgniter query builder: Column 'id' in order clause is ambiguous
Column 'id' in order clause is ambiguous
MySQL error 1052 means the column name exists in more than one joined table, so MySQL cannot pick one.
3 causes, each with a fixBack end
CodeIgniter cannot connect to the MySQL database server
Unable to connect to your database server using the provided settings.
CodeIgniter only reports that its connection failed.
4 causes, each with a fixBack end
CodeIgniter query builder: Unknown column when WHERE uses a SELECT alias
Unknown column 'cnt' in 'where clause'
The error comes from SQL, not from CodeIgniter: a WHERE clause cannot refer to an alias created in the select list, because WHERE is evaluated before that value exists.
3 causes, each with a fix