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,
   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:  WooCommerce Get Product Tax, Shipping & Stock

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:  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