How to join 2 tables in codeigniter php?

Member

by daisha , in category: PHP Frameworks , 7 months ago

How to join 2 tables in codeigniter php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by mallory_cormier , 7 months ago

@daisha 

To join 2 tables in CodeIgniter PHP, you can use the following code:

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

if ($query->num_rows() > 0) {
    foreach ($query->result() as $row) {
        echo $row->column_name;
    }
} 


In this code, 'table1' and 'table2' are the names of the tables you want to join. 'id' is the column in 'table1' and 'table1_id' is the column in 'table2' that you want to join on. You can select specific columns by replacing '*' with the column names you want to retrieve. Finally, you can loop through the query result to access the joined data.