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,
   PRIMARY KEY (id)
);

Models

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

<?php  
class Student extends CI_Controller 
{
	public function __construct()
	{

	parent::__construct();
	

	$this->load->database();
	

	$this->load->model('Student_model');
	}

	public function displaydata()
	{
		$result['data']=$this->Student_model->display_records();
		$this->load->view('display_records',$result);
	}
	
}
?>

Get codeIgniter Data

get all data from db in codeigniter using student_fetch.php

<?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)

<html>
<head>
<title>Display records - www.pakainfo.com</title>
</head>
 
<body>
<table width="600" border="1" cellspacing="5" cellpadding="5">
  <tr style="background:#CCC">
    <th>Sr No</th>
    <th>First_name</th>
    <th>Last_name</th>
    <th>Email Id</th>
	<th>Delete</th>
	<th>Update</th>
  </tr>
  <?php
  $i=1;
  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>";
  $i++;
  }
   ?>
</table>


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

Read Also:  call to undefined function base_url() Fixed

http://your-domainname/codeIgniter/index.php/Student/displaydata

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 infinityknow.com 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:  delete Query In Codeigniter Example Tutorial

About Pakainfo

I am Jaydeep Gondaliya, a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Pakainfo →

Leave a Reply

Your email address will not be published. Required fields are marked *