Select Data
How to Fetch Data from Database

1- Create Controller

2- Create Model

3- Then create HTML page to display data

Create Controller

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends CI_Controller {

public function __construct() {
parent::__construct();
$this->load->database();
$this->load->model('user_model');
}

public function index()
{
$data['user']=$this->user_model->fetch_data();
$this->load->view('display',$data);
}

}

Create Model

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User_Model extends CI_Model {

public function fetch_data()
{
$this->db->select('*');
$q = $this->db->get('tbl_user');
return $q->result();
}

}

HTML page to Display Data

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<html>
<head>
<title>Display Data</title>
</head>
<body>
<table border="1">
<tr>
<td><b>Name</b></td>
<td><b>Email</b></td>
<td><b>City</b></td>
</tr>
<?php
foreach($user as $row){
?>
<tr>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->email; ?></td>
<td><?php echo $row->city; ?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>


Previous
Next