Tags
Asked 3 years ago
17 Jun 2021
Views 242
Joshua

Joshua posted

Select Query in CodeIgniter

Select Query in CodeIgniter
kord

kord
answered Apr 27 '23 00:00

To perform a select query in CodeIgniter, you can use the $this->db->select() and $this->db->get() methods of the database class.

Here's an example of how to perform a select query in CodeIgniter:



$this->db->select('column1, column2, column3');
$this->db->from('my_table');
$this->db->where('column1', $value1);
$this->db->where('column2 >', $value2);
$this->db->order_by('column3', 'ASC');
$query = $this->db->get();

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

In this example, we are selecting columns named "column1", "column2", and "column3" from a table named " my_table ". We are then applying two where conditions to the query using $this->db->where(), sorting the results in ascending order by "column3" using $this->db->order_by() , and retrieving the results using $query = $this->db->get().

Finally, we are checking if the query has any rows using $query->num_rows() > 0 , and looping through the results using a foreach loop to echo the values of each column.

You can modify the example to suit your specific query needs, changing the table name, column names, and where conditions as necessary. Note that you should properly sanitize any user input to prevent SQL injection vulnerabilities
Post Answer