Codeigniter

Database Query in Codeigniter

Pinterest LinkedIn Tumblr

1) Configure database connection
Application/config/database.php

Hostname, Username, Password, Database name

$db['default'] = array(
    'dsn'    => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => 'root',
    'database' => 'code3',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

2) Create controller and model name is Home_model
Here, I have created a controller name Home.


 <?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends CI_Controller {
    
    public function index()
    {
        $this->load->model('Home_model');
        $data['result'] = $this->Home_model->header();
        // print_r($data['result']);
        $this->load->view('admin/index',$data);
    }
}

3) Now I am adding in the model. the table name is users

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home_model extends CI_model
{
    public function header()
    {
        $query = $this->db->get('users');
        return $query->result();
    }
}

Write A Comment