Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use database codeigniter class without codeIgniter MVC framework?

My goal is to use the CodeIgniter database Class, but inside a plain PHP script. I don't want to use MVC structure for this.

Can I use this database class WITHOUT using the CodeIgniter MVC pattern?

If yes, how?

like image 274
yarek Avatar asked Mar 09 '13 11:03

yarek


1 Answers

Please follow below steps to integrate Codeigniter DB Active Record

  1. Download latest codeigniter from https://codeigniter.com/
  2. Copy below files

    application/config/config.php
    application/config/database.php
    
    system/database/*
    
    system/core/Common.php
    system/core/Exceptions.php
    system/core/Log.php
    

    enter image description here

The directory structure is as above

  1. Add database connection details in application/config/database.php
  2. Create db connection(connector.php) file

    <?php
    defined('DS') OR define('DS', DIRECTORY_SEPARATOR);
    defined('EXT') OR define('EXT', '.php');
    defined('ENVIRONMENT') OR define('ENVIRONMENT', 'development');
    
    $dir_path = dirname(__FILE__) . DS;
    defined('BASEPATH') OR define('BASEPATH', $dir_path . 'system' . DS);
    defined('APPPATH') OR define('APPPATH', $dir_path . 'application' . DS);
    
    function getDBConnector(){
        include_once(BASEPATH . "core/Common.php");
        include_once(BASEPATH . "core/Exceptions.php");
        require_once(BASEPATH . 'database/DB' . EXT);
        $conn = & DB();
        return $conn;
    }    
    
    $db = getDBConnector();
    
    print_r($db->get('users')->result_array());
    
  3. Now include connector.php in your project and access DB object :)
like image 121
Simhachalam Gulla Avatar answered Oct 20 '22 21:10

Simhachalam Gulla