Introduction
In this example, we will discuss how to retrieve a record or data from the MySQL database using the CodeIgniter.
In MySQL the SELECT statement is used to retrieve data from one or more tables:
Syntax:
SELECT column_name(s) FROM table_name
or
We can use the * character to retrieve ALL columns from a table:
SELECT * FROM table_name
Controller: User.php application\controllers\User.php
Model: User_model.php application\models\User_model.php
View: list.php application\views\list.php
applications/controllers/User.php
class User extends CI_Controller {
public function __construct() {
/*call CodeIgniter's default Constructor*/
parent::__construct();
/*load model*/
$this->load->model('User_model');
}
public function list() {
$data[‘result’] = $this->user_model->list();
$this->load->view(‘list’, $data);
}
}
applications/models/User_model.php
class User_model extends CI_Model {
/*Select*/
function list() {
$this->db->select([“*”]);
$this->db->from(‘user_info’);
$query = $this->db->get();
return $query->result();
}
}
applications/views/list.php
<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($result as $row) {
echo "<tr>";
echo "<td>".$i. "</td>";
echo "<td>".$row->first_name."</td>";
echo "<td>".$row->last_name."</td>";
echo "<td>".$row->email."</td>";
echo "</tr>"; $i++; } ?>
</table>