Display Records from Database Using Php Codeigniter

Display Records from Database Using Php Codeigniter

By Yashwant Chavan, Views 87537, Date 04-Oct-2014

This php codeigniter tutorial help you to display the records from database table using model view and controller approach.

tags codeigniter php-codeigniter codeigniter-mvc php

Popular Codeigniter Tutorials

Tools and Technologies

To execute Php codeigniter Application I have used below technologies.

  1. Php Codeigniter 1.7.3
  2. XAMPP / Php 5.2.3
  3. Mysql 5.0.4

Database table trn_employee

Create trn_employee table with the help of below mysql table script and insert few records in the table. Once you are done with the table creation in mysql database, Our next step is we need to setup Php Codeigniter project.

CREATE TABLE  `technicalkeeda`.`trn_employee` (
  `employee_id` bigint(20) NOT NULL auto_increment,
  `first_name` varchar(50) collate latin1_general_ci default NULL,
  `last_name` varchar(50) collate latin1_general_ci default NULL,
  `email` varchar(30) collate latin1_general_ci NOT NULL,
  PRIMARY KEY  (`employee_id`)

I am using Codeigniter Version # 1.7.3 to develop the this application, You can use the latest version also. Simple Codeigniter application project looks like below in Eclipse editor.

Display records using codeigniter eclipse setup

Codeigniter Database Configuration

As you know that Php Codeigniter is popular for minimum configuration (almost Zero configuration) framework. To connect to database you need to provide username, password and database name configuration details in database.php file.

The config file is located at "application/config/database.php". Like in my case my local mysql database setting is as below.

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "technicalkeeda";

Codeigniter Base Url Configuration

$config['base_url'] = "http://localhost/codeigniterexamples";


Lets creates the Controller class called Home.php under folder name application/controllers along with default method name index().

class Home extends Controller {

 function Home(){


 public function index(){

  $query = $this->HomeModel->getEmployees();
  $data['EMPLOYEES'] = null;
   $data['EMPLOYEES'] =  $query;

  $this->load->view('index.php', $data);


Lets create the Model class which will query the database table and fetch the records. Navigate to the application/models folder of your CodeIgniter project and create a new php file called HomeModel.php.

Define the getEmployees() method, which reads the records from trn_employee table.

class HomeModel extends Model {

 function HomeModel(){

 function getEmployees(){
  $query = $this->db->get();
  return $query->result();


Lets create index.php file under folder name application/views. Here we are going to display the employee information.

<!DOCTYPE html>
<html lang="en">
  <title>Display Records From Database Using Codeigniter</title>
  <link href="<?= base_url();?>css/bootstrap.css" rel="stylesheet">
  <div class="row">
   <div style="width:500px;margin:50px;">
    <h4>Display Records From Database Using Codeigniter</h4>
    <table class="table table-striped table-bordered">
     <tr><td><strong>Employee Id</strong></td><td><strong>First Name</strong></td><td><strong>Last Name</strong></td><td><strong>Email</strong></td></tr> 
     <?php foreach($EMPLOYEES as $employee){?>
        <?php }?>  


Enter the url http://localhost/codeigniterexamples in your browser, It will display the employee records like below

display records using codeigniter output


Hi there! I am founder of technicalkeeda.com and programming enthusiast. My skills includes Java,J2EE, Spring Framework, Nodejs, PHP and lot more. If you have any idea that you would want me to develop? Lets connect: yashwantchavan[at][gmail.com]