About Lesson
The result() and row() method is used to retrieve the data from the database. result() method returns the list of data objects and the row() method returns the single row.
Basic Syntax to retrieve data from MySQL:
SELECT column_name(s) FROM table_name
or
We can use the * character to retrieve ALL columns from a table:
SELECT * FROM table_name
1. Create a list.php file in applications/views/ directory
<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>
2. Create a model file User_model.php in the applications/models/ directory.
class User_model extends CI_Model { /*Select*/ function list() { $this->db->select([“*”]); $this->db->from(‘user_info’); $query = $this->db->get(); return $query->result(); } }
3. Create a controller file User.php in the applications/controllers/ directory.
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); } }