In this example, you learn to get all data from the database in CodeIgniter, Codeigniter gets data from the database, How to get data from the database in CodeIgniter.


Step 1 : Create Controller

In this step we will create Demo.php controller for get all data from database.

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Demo extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('demo_model');
        $this->now_time = date('Y-m-d H:i:s');
    }
    
    function get_all_data(){
        $demo = $this->demo_model->get_all_records('demo','created_at','desc');
        echo "<pre>";
        print_r($demo);exit;
    }
    
}


Step 2 : Create Model

In this step we will create Demo_model.php model file for create query get all data from database.

<?php

class Demo_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }

    /**
     * @param $table_name
     * @param $order_by_column
     * @param $order_by_value
     * @return bool
     */
    function get_all_records($table_name,$order_by_column,$order_by_value){
        $this->db->select("*");
        $this->db->from($table_name);
        $this->db->order_by($order_by_column,$order_by_value);
        $query = $this->db->get();
        if ($query->num_rows() > 0){
            return $query->result();
        }else{
            return false;
        }
    }
}

I hope you understand of get all data from database in codeigniter and it can help you..