In CodeIgniter, the result()
and row()
methods are commonly used to retrieve data from a database:
result()
: Returns a list of data objects (multiple rows).row()
: Returns a single row as an object.
Basic Syntax to Retrieve Data from MySQL
SELECT column_name(s) FROM table_name
Or, to retrieve all columns from a table:
SELECT * FROM table_name
Step-by-Step Implementation
Below, we’ll create a simple example demonstrating how to retrieve data from a database and display it in a table format.
File Structure
application/
│
├── controllers/
│ └── User.php
│
├── models/
│ └── User_model.php
│
└── views/
└── list.php
Step 1: Create list.php
in application/views/
The list.php
view file will display the fetched data in a table format.
File: application/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 $key => $row) { echo "<tr>"; echo "<td>".++$key."</td>"; echo "<td>".$row->first_name."</td>"; echo "<td>".$row->last_name."</td>"; echo "<td>".$row->email."</td>"; echo "</tr>";
//$i++;
} ?> </table>
Explanation
- The table displays a list of users fetched from the database.
- Each row contains the user’s details along with “Delete” and “Update” options linked to the respective methods in the controller.
Step 2: Create User_model.php
in application/models/
The User_model
handles fetching data from the database using CodeIgniter’s Query Builder.
File: application/models/User_model.php
class User_model extends CI_Model { /*Select*/ public function list() { $this->db->select([“*”]); $this->db->from(‘user_info’); $query = $this->db->get(); return $query->result(); } }
Explanation
$this->db->select('*')
: Select all columns from the table.$this->db->from('user_info')
: Specifies the table from which to fetch data.$this->db->get()
: Executes the query.return $query->result()
: Returns the result set as an array of objects.
Step 3: Create User.php
in application/controllers/
The User
controller manages the flow between the model and the views.
File: application/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); } }
Explanation
__construct()
: Loads theUser_model
when the controller is instantiated.list()
: Calls thelist()
method from theUser_model
to fetch data and pass it to thelist.php
view.
Summary
- Views: Created
list.php
to display a table of users with options to delete or update. - Model: Created
User_model.php
to interact with the database and fetch records usingresult()
. - Controller: Created
User.php
to handle user requests, fetch data using the model, and load the views.
Key Points
result()
is used when you want to fetch multiple rows from the table.row()
is used when you need to fetch a single row from the result set (not used in this tutorial but can be implemented similarly).