Welcome Guest, Not a member yet? Register   Sign In
An easier way to manage multiple database settings!
#1

[eluser]talldarkmystere[/eluser]
Quote:This post deals with the database config file.

CI 1.7.2: /system/application/config/database.php

CI 2.0 /application/config/database.php

I've found that when working with multiple databases, it's easier to use an actual array than the given format:

Current database array (CI 1.7.2)
Code:
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "";
$db['default']['password'] = "";
$db['default']['database'] = "";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

So, I rewrote my database arrays as actual arrays:

Codeigniter 1.7.2
Code:
$db['default'] = Array(
    'hostname' => 'localhost',
    'username' => '',
    'password' => '',
    'database' => '',
    'dbdriver' => 'mysql',
    'dbprefix' => '',
    'pconnect' => TRUE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci'
);

Codeigniter 2.0
Code:
$db['default'] = Array(
    'hostname' => 'localhost',
    'username' => '',
    'password' => '',
    'database' => '',
    'dbdriver' => 'mysql',
    'dbprefix' => '',
    'pconnect' => TRUE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'autoinit' => TRUE,
    'stricton' => FALSE
);

The benefit of having the database arrays set up in this manner is that it makes adding new arrays easier; you don't have to copy and paste the section and then change every line to reflect the new name:
Code:
$db['connection name']['property'] = 'property';

Using arrays like this, it's still quite readable, and now you've only got to change the name of the array once.[/quote]
#2

[eluser]talldarkmystere[/eluser]
Forum Mods: I realize that this post is probably better suited to the Ignited Code forum.

Would you mind moving it?

Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB