🔥 CodeIgniter Intermediate

What is CodeIgniter 4's Query Builder?

Why Interviewers Ask This

This tests whether you can apply CodeIgniter knowledge to real-world scenarios. Interviewers are looking for clarity of thought and evidence that you've encountered this in production code.

Answer

The Query Builder (formerly Active Record in CI3) is CI4's fluent interface for constructing database queries. Access from a model: $this->db or $db = \Config\Database::connect(). Methods: $db->table("users")->select("name, email")->where("active", 1)->orWhere("role", "admin")->whereIn("department", [1, 2])->like("name", "Al")->orderBy("name", "ASC")->limit(10, 20)->get(). Join: ->join("profiles", "profiles.user_id = users.id", "left"). Group by: ->groupBy("department")->having("count(*) >", 5). Insert: $db->table("users")->insert($data). Batch insert: ->insertBatch($rows). Update: ->where("id", $id)->update($data). The Query Builder automatically escapes values, preventing SQL injection. Unlike Eloquent, it does not return objects by default — use getResultArray() for arrays or getResult() for objects.

Common Mistake

Don't just define the term — demonstrate that you understand when to use it and when not to. Showing awareness of trade-offs is what separates average from strong CodeIgniter candidates.