Active Record – read (select)

The R of CRUD represents the process to select data from a database. CodeIgniter uses the $this->db->get() database function to fetch rows from the database. Its usage is explained in the following sections.

Getting ready

The following is the SQL code required to support this recipe; you'll need to adapt it to your circumstances.

CREATE TABLE IF NOT EXISTS `ch6_users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `firstname` varchar(50) NOT NULL,
  `lastname` varchar(50) NOT NULL,
  `username` varchar(20) NOT NULL,
  `password` varchar(20) NOT NULL,
  `created_date` int(11) NOT NULL,
  `is_active` varchar(3) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

INSERT INTO `ch6_users` (`firstname`, `lastname`, `username`, `password`, `created_date`, `is_active`) VALUES
('claire', 'Strickland', 'cstrickland', 'password', 1366114115, 'yes'),
('Douglas', 'Morrisson', 'dmorrisson', 'password', 1366114115, 'yes'),
('Jessica', 'Welsh', 'jesswelsh', 'password', 1366114115, 'yes'),

How to do it...

We're going to create the following two files (or amend those files if you have already created them):

  • /path/to/codeigniter/application/controllers/database.php
  • /path/to/codeigniter/application/models/database_model.php

The following steps will demonstrate how to read data into a database using CodeIgniter Active Record:

  1. Add the following code into the file /path/to/codeigniter/application/controllers/database.php:
      public function select_row() { 
        $id = 1; 
        $this->load->model('Database_model'), 
        $result = $this->Database_model->select_row($id);  
        echo '<pre>'; 
    
        var_dump($result->result()); 
      } 
  2. Add the following code into the file /path/to/codeigniter/application/models/database_model.php:
    function select_row($id) {	 
      $this->db->where('id', $id); 
      $query = $this->db->get('ch6_users'), 
      return $query; 
    } 

You know it has worked if you see the following output:

array(1) { 
  [0]=> 
  object(stdClass)#20 (7) { 
    ["id"]=> 
    string(1) "1" 
    ["firstname"]=> 
    string(4) "Lucy" 
    ["lastname"]=> 
    string(5) "Welsh" 
    ["username"]=> 
    string(6) "lwelsh" 
    ["password"]=> 
    string(8) "password" 
    ["created_date"]=> 
    string(10) "1366114115" 
    ["is_active"]=> 
    string(3) "yes" 
  } 
} 

How it works...

In the preceding controller, public function select_row() assigns $id with the value 1—however, this can also be done from post, get, session, or another source—and loads the database model, passing the variable $id to it as follows:

    $this->load->model('Database_model'), 
    $this->Database_model->insert_batch_data($data);  

The model function select_row() pulls the matching record from the table 'ch6_users' and returns it to the calling controller.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.74.54