How to join tables in codeigniter?

Member

by jasen , in category: PHP Frameworks , 4 months ago

How to join tables in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by domenico , 21 days ago

@jasen 

To join tables in CodeIgniter, you can use the active record feature provided by CodeIgniter's database library. Here is an example of how you can join two tables in CodeIgniter:

1
2
3
4
5
6
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2', 'table1.id = table2.table1_id');
$query = $this->db->get();

$result = $query->result();


In this example, we are selecting all columns from "table1" and joining it with "table2" on the condition that "table1.id = table2.table1_id". Once the query is executed, you can fetch the result using $query->result().


You can also specify the type of join (inner join, left join, right join) by passing it as the third parameter in the join method. For example, to do a left join:

1
$this->db->join('table2', 'table1.id = table2.table1_id', 'left');


This is how you can join tables in CodeIgniter using the active record feature. It provides a convenient and secure way to build complex queries with ease.