how to fetch data from database in codeigniter?

Today, We want to share with you how to fetch data from database in codeigniter.In this post we will show you insert and display data from database in codeigniter, hear for select query in codeigniter we will give you demo and example for implement.In this post, we will learn about codeigniter count rows in view & controller with an example.

Retrieve data from database using CodeIgniter framework

In this example we will learn all about how to fetch a record or data from MySQL database using CodeIgniter framework PHP.

Sql Table

CREATE TABLE students (
  `id` int(11) NOT NULL,
  `profile_nm` varchar(30) NOT NULL,
  `sir_nm` varchar(30) NOT NULL,
  `email` varchar(30) NOT NULL,


Student.php (Path: application\models\Student_model.php)

class Student extends CI_Controller 
	public function __construct()




	public function displaydata()

Get codeIgniter Data

get all data from db in codeigniter using student_fetch.php

class Student_model extends CI_Model 
	function display_records()
	$query=$this->db->query("select * from students");
	return $query->result();

Display records

here simple you can display data with codeigniter view file like as a display_records.php (Path: application\views\insert.php)

<title>Display records -</title>
<table width="600" border="1" cellspacing="5" cellpadding="5">
  <tr style="background:#CCC">
    <th>Sr No</th>
    <th>Email Id</th>
  foreach($data as $student)
  echo "<tr>";
  echo "<td>".$i."</td>";
  echo "<td>".$student->profile_nm."</td>";
  echo "<td>".$student->sir_nm."</td>";
  echo "<td>".$student->email."</td>";
  echo "</tr>";

and last step Now run the example on your browser like chrome with the below URL:


After here you can simply retrive the students data the table look like this.

Id Profile name Sir name Email Id
1 Virat Kohali [email protected]
2 Bhavika Pethani [email protected]
3 Sejal Ramani [email protected]

I hope you get an idea about how to fetch data from database in codeigniter.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.