In this article, we will explain how to use a select query with multiple clauses in Codeigniter. The Codeigniter provides Active Record class and that class through we can get data from the database using a select query.

CodeIgniter Select Query Example

$this->db->select("*");
$this->db->from("users");
$objQuery = $this->db->get();
$data = $objQuery->result_array();

Codeigniter select query with get where clause

$this->db->select("*");
$this->db->from("users");
$this->db->where('id',2);
$objQuery = $this->db->get();
$data = $objQuery->result_array();

Codeigniter Select Query with Multiple Where Clause

$this->db->select("*");
$this->db->from("users");
$this->db->where('id',1);
$this->db->where('first_name','test');
$objQuery = $this->db->get();
$data = $objQuery->result_array();

Codeigniter select query with Or Where clause

$this->db->select("*");
$this->db->from("users");
$this->db->or_where('first_name', 'test');
$objQuery = $this->db->get();
$data = $objQuery->result_array();

Codeigniter select query with Where In clause

$this->db->select("*");
$this->db->from("users");
$this->db->where_in('id',array(1,2));
$objQuery = $this->db->get();
$data = $objQuery->result_array();

Codeigniter select query with Where Not In clause

$this->db->select("*");
$this->db->from("users");
$this->db->where_not_in('id',array(1,2));
$objQuery = $this->db->get();
$data = $objQuery->result_array();

Codeigniter select query with Like clause

$this->db->select("*");
$this->db->from("users");
$this->db->like('first_name', 's', after);
$objQuery = $this->db->get();
$data = $objQuery->result_array();

Codeigniter select query with Limit clause

$this->db->select("*");
$this->db->from("users");
$this->db->limit(5, 10);
$objQuery = $this->db->get();
$data = $objQuery->result_array();

Codeigniter select query with Join clause

You can refer to this URL for Codeigniter select query with join clause.

Codeigniter select query with Order By clause

$this->db->select("*");
$this->db->from("users");
$this->db->order_by('id', 'desc');
$objQuery = $this->db->get();
$data = $objQuery->result_array();