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.

Read Also:  How to find the Json Object Length in Javascript?

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:

Read Also:  WooCommerce Get Product Tax, Shipping & Stock


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.

Read Also:  PHP multi dimensional array sorting by a column with example

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  How to Get Current Date and Time in PHP?
  2. Read Also:  Triangle Pattern Program In PHP
  3. Read Also:  insert Query In Codeigniter Example Tutorial
  4. Read Also:  Laravel 7 jQuery Ajax Image Upload Tutorial
  5. Read Also:  Find duplicate data index in php Examples
  6. Read Also:  codeigniter print last query - PHP Examples
  7. Read Also:  PHP Webcam Capture Image Save from Camera using jQuery
  8. Read Also:  REST API CRUD Example in PHP with MySQLi
  9. Read Also:  Update Query In Codeigniter Example Tutorial
  10. Read Also:  PHP Ternary shorthand If/Else Examples