PHP CODE IGNITER TUTORIAL

How To Fetch data from a database Using PHP Codeigniter

Written by admin

In this tutorial, we’ll understand the way to display(fetch) data from database and display records insight. within the Previous example, we saved records inside tblusers table.
In this example, we’ll use tblusers table to display records.

Table structure for table tblusers
CREATE TABLE `tblusers` (
  `id` int(11) NOT NULL,
  `name` varchar(45) DEFAULT NULL,
  `email` varchar(45) DEFAULT NULL,
  `phone` varchar(45) DEFAULT NULL,
  `gender` varchar(45) DEFAULT NULL,
  `create_date` timestamp NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
2-Database configuration
insertci/application/config/database.php
$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
   'dsn'   => '',
   'hostname' => 'localhost', //localhost
   'username' => 'root', //username
   'password' => '',  //password
   'database' => 'student_info', //databasename
   'dbdriver' => 'mysqli',
   'dbprefix' => '',
   'pconnect' => FALSE,
   'db_debug' => (ENVIRONMENT !== 'production'),
   'cache_on' => FALSE,
   'cachedir' => '',
   'char_set' => 'utf8',
   'dbcollat' => 'utf8_general_ci',
   'swap_pre' => '',
   'encrypt' => FALSE,
   'compress' => FALSE,
   'stricton' => FALSE,
   'failover' => array(),
   'save_queries' => TRUE
);
3-Add to Libraries and helper
insertci/application/config/routes.php
$autoload['libraries'] = array('database','session','form_validation');
$autoload['helper'] = array('html','url','form');
4-Create a Model(Showrecord.php)
insertci/application/models/Showrecord.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Showrecord extends CI_Model{

	public function fetchrecord()
		{
			$query=$this->db->select('id,name,email,phone,gender')
			->get('tblusers');
			return $query->result();
		}
} 
5-Create a Controller(Showdata.php)
insertci/application/controllers/Showdata.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Showdata extends CI_Controller {

	public function index()
	{
	$this->load->model('Showrecord');
     $results=$this->Showrecord->fetchrecord();
     $this->load->view('fetchdata',['result'=>$results]);
	}
}
6-Create a View data(fetchdata.php)
insertci/application/views/fetchdata.php
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
  <h2>Fetchdata from Database</h2>
       <table class="table table-bordered">
                   <thead>
                    <tr>
                      <th>#</th>
                      <th>Name</th>
                      <th>Email</th>
                      <th>Phone</th>
                      <th>Gender</th>
                    
                     </tr>
                  </thead>
              <?php
if(count($result)) {
$cnt=1;
foreach ($result as $row){
?>
<tr>
<td><?php echo htmlentities($cnt);?></td>
<td><?php echo htmlentities($row->name)?></td>
<td><?php echo htmlentities($row->email)?></td>
<td><?php echo htmlentities($row->phone)?></td>
<td><?php echo htmlentities($row->gender)?></td>
</tr>
<?php
$cnt++;
} // end foreach
} else { ?>
<tr>
<td colspan="7">No Record found</td>
</tr>
<?php
}
?>
</tbody>
  </table>
</div>
</body>
</html>

About the author

admin

Hi there! I'm Shiv Gupta. I specialized in building Websites. I write blogs in my free time. I really like to find out and share my knowledge with others.I founded ThePhpConcept in March 2020. I started this blog in order that I can interact with some like-minded people and also help people learning PHP, Mysql, jQuery, Html, and PHP Projects and related technologies.
Support-thephpconcept@gmail.com

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.