Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

CodeIgniter: Call to undefined method on the result object when using order_by() after get_where()

ErrorCall 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().

  • Applies to CodeIgniter 3.1, CodeIgniter 4.5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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/
  2. 2Match what you see, then jump to its fix

    1. 1The failing line looks like ->get_where('table', $where)->order_by(...)order_by() is chained after the query has already runFix
    2. 2The 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. 3->getWhere([...])->orderBy(...) in a CodeIgniter 4 model or builderThe same mistake with getWhere() in CodeIgniter 4Fix

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

Did this fix it?

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/ | head

2Fix

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, 10

3Verify

Print the generated SQL and check the ORDER BY and LIMIT parts.

echo $this->db->last_query();

Confirmed by codeigniter.com

Did this fix it?

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/ | head

2Fix

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

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.