How to use distinct in codeigniter?

by muriel.schmidt , in category: PHP Frameworks , 5 months ago

How to use distinct in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by dedrick , 5 months ago

@muriel.schmidt 

In CodeIgniter, you can use the distinct() method in the database query builder to retrieve only distinct rows from a table. Here is an example of how to use distinct() in CodeIgniter:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$this->db->select('column_name');
$this->db->distinct();
$this->db->from('table_name');

$query = $this->db->get();

if ($query->num_rows() > 0) {
    foreach ($query->result() as $row) {
        // Process the results
    }
}


In this example, select() method is used to specify the column that you want to retrieve. Then, distinct() method is used to retrieve only distinct rows based on that column. Finally, you can execute the query using get() method and loop through the results if there are any.