Fetch Data From Database With CodeIgniter Framework in PHP


In the previous article you saw how to create an application with the CodeIgniter framework in PHP. In this article you will see how to fetch data from a MySql database through the CodeIgniter framework in PHP. Suppose that we have the following table. We want this data fetched from a database with the CodeIgniter framework in the PHP front end.

3rd.jpg

First of all we will create a database and table in MySql. Suppose we created the "Company" database and employee table in MySql. There are the three fields id, name and city in the employee table. After creating the database and table in MySql we will set the database configuration in the CodeIgniter framework. For database configuration we will go to c:\xampp\htdocs\CodIgniter\ Application\Config\database.php. Then we will write following code in the database.php file:

$db['default']['hostname'] = 'localhost';
$db[
'default']['username'] = 'root';
$db[
'default']['word'] = '';
$db[
'default']['database'] = 'Company';//Company is a database

After configuration of the database in the Codeigniter framework we will create the controller function. To create the controll function we will go to c:\xampp\htdocs\CodIgniter\System\Core\Controller.php. Then write the following code in the controller.php:

class CI_Controller { 
      private static $instance;
     
/**
       * Constructor
       */
      public function CI_Controller()
      {
           
self::$instance =& $this;
           
// Assign all the class objects that were instantiated by the
            // bootstrap file (CodeIgniter.php) to local class variables
            // so that CI can run as one big super object.
            foreach (is_loaded() as $var => $class)
            {
                  $this->$var =& load_class($class);
            }
            $this->
load =& load_class('Loader', 'core');
            $this->
load->initialize();
           
            log_message(
'debug', "Controller Class Initialized");
      }
     
public static function &get_instance()
      {
           
return self::$instance;
      }
}

After creating the controller function in the CodeIgniter framework we will create a model function. To create the model function we will go to c:\xampp\htdocs\CodIgniter\System\Core\Model.php. Then write the following code in the model.php file:

class CI_Model {
     
/**
       * Constructor
       *
@access public
       */
      function CI_Model()
      {
            log_message(
'debug', "Model Class Initialized");
      }
     
/**
       * __get
       * Allows models to access CI's loaded classes using the same
       * syntax as controllers.
       *
@param   string
       *
@access private
       */
      function __get($key)
      {
            $CI =& get_instance();
           
return $CI->$key;
      }
}

After creating the controller and model function we will create a model file:

<?php
class Emp_model extends CI_Model
{
 
function Emp_model()
  {
   
parent::CI_Model();
  }
 
function emp_getall()
  {
       $this->load->database();
       $query=$this->db->get(
'employee');//employee is a table in the database
       return $query->result();
  }
}
?>

The above file will be saved as emp_model.php in the c:\xampp\htdocs\CodIgniter\Application\Models.

After creating the model file we will create a view file. For creating the view file we will write the following code:

<?php
foreach($query as $row)
{
 
print $row->ID;
 
print $row->Name;
 
print $row->City;
 
print "<br>";
}
?>

The above file will be saved as emp_viewall.php in the c:\xampp\htdocs\CodIgniter\Application\Views. After creating the views file we will create the controller file. To create the controller file we will write the following code:

<?php
class emp extends CI_Controller
{
 
function emp()
  {
    
parent::CI_Controller();
  }
 
function GetAll()
  {
     $this->load->model(
'emp_model');
       $data['query']=$this->emp_model->emp_getall();
       $this->load->view(
'emp_viewall',$data);
  }
}
?>

The above file will be saved as emp.php in the c:\xampp\htdocs\CodIgniter\Application \Controller.

Run the Code

To run the the code we will write the following URL in the web browser:

http://localhost/CodeIgniter/index.php/emp/getall.

2nd.jpg

Conclusion

So in this article you saw how to fetch data from a database using the CodeIgniter framework in PHP. Using this article one can easily understand how to fetch data from a database with the CodeIgniter framework in PHP.

Some Helpful Resources

Up Next
    Ebook Download
    View all
    Learn
    View all