Chat with us, powered by LiveChat

This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

Launch PHP Websites and Applications in seconds through our 1-Click App Install Launch Now!

What is Codeigniter Active Records and How to Use It?

Updated on December 21, 2018

2 Min Read
Codeigniter Active Records
Reading Time: 2 minutes

Active Record is an important concept in PHP project development that removes many of the complexities of using databases in projects. In fact, Active Records is considered the Model in the MVC model.

Active Records is used to separate the business logic and data from the rest of the project and thus removes much of the potential sources of errors that could derail the development. Codeigniter Active Records is an easy-to-use database manipulation mechanism that saves a lot of time for the developers who can accomplish almost all database processing action in a few lines of code.

In this article, I will demonstrate how to use Codeigniter Active Records by creating a Controller, Model and a View.

Create the Database

I will start by creating the database that I will use for this tutorial. In the database.php (located in the application/config/ folder), add the following information:

'hostname' => 'localhost',

   'username' => 'root',

   'password' => '',

   'database' => 'activerecords',

You Might Also Like: Simple MVC Codeigniter CRUD Application

Now in the ‘activerecords’ database, create a table named ‘customer’ through the following query:

CREATE TABLE `customer` (

 `ID` int(255) NOT NULL,

 `Name` text NOT NULL,

 `Email` varchar(255) NOT NULL,

 `Contact` int(255) NOT NULL


Configure the Model

Next, call the Model in main constructor method of the Controller file:

public function __construct()



   $this->load->model('datacustomer', 'customer');


You Might Also Like: Display Data in Codeigniter JSON Format

Display the Data

To display the data extracted/processed by the Model (Codeigniter Active Records in this case), I will create a View file within the View folder. This View file will contain the following code:


       $cw = 1;

       foreach ($data as $actrec):

           echo $actrec->name;

           echo $actrec->email;

           echo $actrec->contact;



At this point, it is important to define the Controller in the routes.php file. For the purpose of this tutorial, I will declare ‘home’ as the default controller. Check out the following code:

$route['default_controller'] = 'home';

You Might Also Like: Pass Data From Controller to View in CodeIgniter

Next, pass the request in the index controller to handle active data records:

public function index()


$data = array();

$data['title'] = 'Home';

$config = array();

$config["base_url"] = base_url().'home/index';

$data["data"] = $this->customer->get_customer();

$this->template->load('default_layout', 'home', $data);


Configure the Model

Next, I will create the Model that will fetch the records from the ‘activerecord’ database. Here’s the code for the ‘datacustomer’ Model:

class datacustomer extends CI_Model {

public function __construct()





function get_customer($active_rec_id=0)



$query = $this->db->get('customer');

if ($query->num_rows() > 0) {

foreach ($query->result() as $row) {

$data[] = $row;


return $data;


return false;




As you can see, using Codeigniter Active Records is very simple. The important fact to remember is that Active Records belong to the Model (data access layer/component) and not in Controller or View.

Here is a sample PHP code snippet that gets the results:

function get_result($active_rec_id){

   $this->db->select('name, email, contact');


   $this->db->join('departments', ' =');

   $this->db->where('id', $active_rec_id);


   return $query->result();


In the above code snippet, `$this->db->selectis`  used to select one or more columns from the table. Similarly, I have used $this->db->from to select one or more tables from the database. Finally, I have used `$this->db->join` to set up joins for tables.


In this short overview of Codeigniter Active Records and demonstrate how you can easily use it to abstract database handling and processing. You can use this tutorial as the basis for building a high performing Model for your Codeigniter project. In case of any issues, leave a comment below.

Share your opinion in the comment section. COMMENT NOW

Shahroze Nawaz

Shahroze is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. Besides his work life, he loves movies and travelling. You can email him at

Launch PHP websites without the worry of Server Management.

Pre-Installed Optimized Stack with Git, Composer & SSH

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!