Fetch data Or Read Operation in CodeIgniter from database

In this example, we will discuss about how to retrieve a record or data from MySQL database using CodeIgniter framework PHP.

 I suppose that you know how to connect database to CodeIgniter and how to insert and the record in the database.

To fetch data of whole table without any filters use the below command

$data = $this->db->get(‘table_name’)->result_array();

Using result_array() it will create an array of all data like :-

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => name
            [email] => name@example.com
        )

    [1] => Array
        (
            [id] => 2
            [name] => name2
            [email] => name2@gmail.com
        )

)
To fetch each data by using foreach function like :-
 foreach ($data as $single_data)
To fetch data of the whole table using any filters or where clause use the below command
$this->db->where(‘field_name’,$value) ;
          $data = $this->db->get(‘table_name’)->result_array();

here also to display records the method is same as above

To fetch any single record using filters or where clause use the below command
$this->db->where(‘field_name’,$value) ;
          $data = $this->db->get(‘table_name’)->row_array();

Here we have use row array because we know that we have only one record to be fetch so we have used row array it will only fetch a single record so we don’t have to use result_array funnction.

Tags: No tags

Leave A Comment

Your email address will not be published. Required fields are marked *