Codeigniter, PHP

Codeigniter Model: How to fetch Data From Database

Codeigniter | Controller | Framework | Model | Mvc | PHP



This entry is part 2 of 8 in the series Learn Codeigniter

In Codeigniter Model is Responsible for database related operations like retrieving records from database, inserting records in database, deleting records or updating records in database. So first we need a database. Let’s create one database in phpmyadmin. Open phpmyadmin create database named cidb (Remember we configure the database.php file) and import following sql in it. before you go through this, make sure you have already read Codeigniter Pass Data to View.

 Create First codeigniter model

Create a new file in codeigniter model directory and name it testmodel.php. put following code in it.

We created a class with first character capital and name is same as its filename. It extends CI_Model (Codeigniter Model Base Class) base class. Next thing is to create a function called getAllRecords which fetch all records from test table. To access database codeigniter has built in library database which we have to load to access its functionality. $this->load->library(“database”) will load database library in $this->db object =. Generally $this->load->library() function creates object with filename of that library but in database library it is db. If we dont want to put this library load line for database, we can add autoload library in config/autoload.php file. $this->db->get is method in database library which will get all records of specified table from database and returns an query object containing useful information. Query object has num_rows() method which we can use to check how many number of records are returned from database. If any then return array of objects otherwise return empty array.

It autoloads database library and url helper (helper will be discussed later when we use it but for now just autoload the url helper) everytime. so we dont need that $this->load->library(‘database’) line in model.

Next thing is to use codeigniter model in our home controller. so modify code for home controller’s index method as follows:

use returned data in view. So modify homeview as follows:

Now navigate to http://localhost:8888/ci/ which loads home controller, which calls testmodel. Testmodel fetches records from database and returns array of objects, which then will print_r by homeview.php. So result will be like:

CodeIgniter Model to fetch Data

As we can see array of objects are printed on screen now we can loop through array and display it in proper way. So open homeview.php and modify it like:

now reload browser to see the result and you will get something like this one.

CodeIgniter Model to fetch Data

as you can see the data is retried using codeigniter model, which then passed by controller to view and then view loaded the data. I hope you find it useful.

Series Navigation<< Codeigniter: Getting Started<< How MVC Architecture WorksCodeigniter: How to Pass Data to View >>

Bookmark Link using: bookmark at folkd

  • mj

    An Error Was Encountered

    Unable to load the requested class: database

  • Dhayal Ram

    its shows an error as Unable to load the requested class: database, i tried copy paste, then also it shows error

    • Ronak Prajapati

      with folowing line

      This will solve error.
      This is a version problem. you might using different Codeigniter Version.

      • Dhayal Ram


  • Pingback: Codeigniter CRUD: Day 1 Create Model » CodeRiddles

  • Swapnil Phadke

    code work perfectly but images are not display.

    I m beginner in codeignitor. my problem is when i fetch data from database all other data has fetched but image are not displayed.please proveide me code for how to fetch image from database.

    model file code


    $q = $this->db->get(‘cardetails’);

    if($q->num_rows() > 0)


    return $q->result();


    return array();




    controller file code


    $data['records'] = $this->car_model->getAllRecords();







    view file code

    <img height="100" width="200" src="http://localhost/ci_user/images.'mco_carimage?&gt;.’


    mco_carname ?>

    mco_totalkm ?>

    mco_price ?> Rs

    mco_insurance ?>

    mco_fualtype ?>

    • Ronak Prajapati

      remove single quote after /images.
      and also add forward slash instead.
      Hope this helps. if not let me know

  • mahipal

    Hi I am biginner in code igninter.
    I want to fatch data from two table in database.

  • sushil

    nice tutorial. Thanks for this…

  • Ibn Shajahan

    nice tutorial. Dear php beginner’s, use ” “instead of ” “to understand how it works.




    instead of




  • mani

    i want to store data in variable after getting data from data base through condition

  • Apeksha

    hello..i have tried this code but it shows some error like undefined variable records.

  • Manda

    Hi thank you for sharing this tutorial. I’ve tried this tutorial and finally I can retrieve data from database :) however I would like to retrieve only one record from my database. How can I do that? I’ve tried to change some code in view, I only type one field that I want to get. But it turned out It gives exact value in database and also value from for each function(on pict there is asd 0). Any idea about this?
    Thanks in advance

  • murali chandra

    i am faceing issue unable load database