Codeigniter's `where` and `or_where`
In CodeIgniter, the where method on the database class is used to add a WHERE clause to your query.
In CodeIgniter, the where method on the database class is used to add a WHERE clause to your query. It can be used in several ways:
Example of using the where method in CodeIgniter
<?php
$this->db->where('name', $name);
// Produces: WHERE name = 'Joe'Example of chaining multiple where conditions
<?php
$this->db->where('name', $name);
$this->db->where('title', $title);
// Produces: WHERE name = 'Joe' AND title = 'boss'Note: CodeIgniter automatically escapes values passed to
where()to prevent SQL injection. This syntax applies to CodeIgniter 3 and 4 Query Builder. To execute the query, call$this->db->get().
You can also use the or_where method in a similar way to add OR clauses to your query:
Example of using the or_where method in CodeIgniter
<?php
$this->db->or_where('name', $name);
// Produces: OR name = 'Joe'Example of combining where and or_where
<?php
$this->db->where('name', $name);
$this->db->or_where('title', $title);
// Produces: WHERE name = 'Joe' OR title = 'boss'You can also pass an array to where and or_where:
Example of passing an array to the where method in CodeIgniter
<?php
$array = ['name' => $name, 'title' => $title];
$this->db->where($array);
// Produces: WHERE name = 'Joe' AND title = 'boss'You can use the where method multiple times to build up a WHERE clause with multiple AND clauses. The or_where method can be used to add OR clauses to the WHERE clause.