In this post, we’re going to learn how to select data from database using CodeIgniter.

Related posts:

You can download the Source code from below link and run on your Local server.

Files to refer:

  • cisite\application\views\examples\select.php
  • cisite\application\controllers\Example.php
  • cisite\application\models\ExSelect.php
  • cisite\application\config\routes.php

Before continuing, create a database named cisite and a table named ExSelect.

CREATE TABLE cisite.ex_select ( id INT NOT NULL , book VARCHAR(100) NOT NULL , author VARCHAR(100) NOT NULL);
INSERT INTO cisite.ex_select VALUES(1, 'Wings of fire', 'Dr. APJ Abdul Kalam');
INSERT INTO cisite.ex_select VALUES(2, 'War and Peace', 'Tolstoy');
INSERT INTO cisite.ex_select VALUES(3, 'Jungle Book', 'R. Kippling');
INSERT INTO cisite.ex_select VALUES(4, 'Das Capital', 'Karl Marks');
INSERT INTO cisite.ex_select VALUES(5, 'My Experiments With Truth', 'M. K. Gandhi');

1) Create a view: select.php and paste the following code:

<!DOCTYPE html>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>CI Select</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<form action="selectdata" method="POST">
<input type="text" name="book" list="book" placeholder="Select a book">
<button>Find Author</button>

<datalist id="book">
<option value="Wings of fire">
<option value="War and Peace">
<option value="Jungle Book">
<option value="Das Capital">
<option value="My Experiments With Truth">

2) Create a controller: Examples.php and paste the following code:

defined('BASEPATH') OR exit('No direct script access allowed');
class Examples extends CI_Controller{

public function index($page="index")
switch ($page) {
case 'selectdata':
case 'select':

public function SelectData()

echo $this->ExSelect->SelectData($book);

3) Create a model: ExSelect.php and paste the following code:

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

class ExSelect extends CI_Model{

public function __construct()

public function SelectData($val)
$condition = "book='".$val."'";

foreach($res->result() as $row)
return $result;

If you have not created a new routing, create one as shown below:

Add this to routes.php:

//Examples start
$route['examples'] = 'Examples';
$route['examples/(:any)'] = 'Examples/index/$1';
//Examples End