10

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?

user115014
  • 922
  • 14
  • 23
yarek
  • 11,278
  • 30
  • 120
  • 219
  • Why do you want to do this? – j0k Mar 09 '13 at 11:09
  • you can use php include to include the db class – Muhammad Raheel Mar 09 '13 at 11:10
  • 4
    How does this have 3 upvotes? – Qix - MONICA WAS MISTREATED May 13 '14 at 21:42
  • What part of the class do you want to use? Is it for database connections or using Active Record? – kchason Jun 02 '14 at 12:45
  • @Qix, why do you ask? What's wrong with the question? – Mischa Sep 26 '14 at 05:01
  • I can't speak for @Qix on this but my take is that firstly, usually the best questions show that you have at least tried yourself and come to us with a more specific problem, in which case you can include some example of your code. Secondly, there are so many standalone DBAL and ORM solutions out there that it seems a little like hard work to do what you ask, it's not something anyone here can reasonably expect to answer within 30mins or so. If it takes more than that and isn't interesting to someone who is willing to answer the question then your chances of good answers is quite low. – dops Nov 22 '15 at 23:15
  • To continue, I would probably mark this as too vague if I were reviewing this question in the triage queue. See this guide http://stackoverflow.com/help/how-to-ask for some helpful information. – dops Nov 22 '15 at 23:18

1 Answers1

7

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 :)
  • geeting below ERROR: Fatal error: Call to undefined function get_instance() in G:\wamp\www\CIDB\system\core\Lang.php on line 139 – nilesh Feb 06 '16 at 11:33