Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Derek Jones edited this page · 24 revisions
Clone this wiki locally


(Originally discussed here) Here's an ADOdb library for use in CI.

What are the benefits of Adodb? It's mature, and supports lots of databases. I really like some of the features in it, autoexecute(), and getrow() are particularly useful.


  1. Grab ADODB
  2. Unzip to system/application/libraries/adodb/
  3. Optionally apply ConfigLoadPatch (as of CI 1.3.3, this is not included), otherwise you'll have to create a valid config/adodb.php or you'll get a CI error (if you apply the patch, ADOdb will be able to detect that config/adodb.php doesn't exist and instead read your config/database.php file).
  4. Add the following source code to the appropriate directory



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

if (!function_exists('_init_adodb_library')) {
    function _init_adodb_library(&$ci) {
        $db_var = false;
        $debug = false;

        // try to load config/adodb.php
        // extra parameter comes from patch at
        // without this patch, if config/adodb.php doesn't exist, CI will display a fatal error.
        if ($ci->config->load('adodb',true)) {
            $cfg = $ci->config->item('adodb');
            if (isset($cfg['dsn'])) {
                $dsn = $cfg['dsn'];

            // set db_var if it's set in the config file, or false otherwise
            $db_var = isset($cfg['db_var']) && $cfg['db_var'];

            $debug = isset($cfg['debug']) && $cfg['debug'];

        if (!isset($dsn)) {
            // fallback to using the CI database file
            $group = 'default';
            $dsn = $db[$group]['dbdriver'].'://'.$db[$group]['username']

        // $ci is by reference, refers back to global instance
        $ci->adodb =& ADONewConnection($dsn);

        if ($db_var) {
            // also set the normal CI db variable
            $ci->db =& $ci->adodb;

        if ($debug) {
            $ci->adodb->debug = true;

if ( ! class_exists('ADONewConnection') )

$obj =& get_instance();
$obj->ci_is_loaded[] = 'adodb';



By default, it will create an "adodb" object ($this->adodb) that is connected to the database defined in application/config/database.php (currently hardcoded to use the 'default' group). Alternatively, you can create an application/config/adodb.php file:

$config['adodb']['dsn'] = 'mysql://user:pass@localhost/dbname';
$config['adodb']['db_var'] = true;
$config['adodb']['debug'] = false;

All values are optional.

dsn can be any valid DSN string, as described at If not specified, the settings in config/database.php are used. db_var if true will set $this->db to be a reference to $this->adodb, overwriting CodeIgniter's database object. Defaults to false. debug sets ADOdb debug mode, defaults to false.

You can get going very fast:

$row = $this->adodb->getrow('SELECT * FROM users WHERE user_id = 1');
echo 'hello '.$row['firstname'];

You can also of course auto-load this like any other library, by adding it to the 'core' array in application/config/autoload.php:

$autoload['core'] = array('adodb');


Patch Notice

Note that you need the patch described at this thread for the init to work, or alternatively, you can create a config/adodb.php file, or comment out the whole IF block where it mentions that patch url.

In the future..

Something that may be useful is to write wrappers for the ADOdb object that emulate all the functions in the CI database class, as well as the QueryResult class. Now, this adds abstraction to an abstraction layer.. so it may be unnecessarily complex, not to mention you kind of defeat the purpose of using ADOdb.

I have not yet played with the ADOdb active record stuff, but it may be possible to integrate it with models somehow.


Something went wrong with that request. Please try again.