Welcome Guest, Not a member yet? Register   Sign In
Database config settings
#1

[eluser]Sean Downey[/eluser]
Hello

I'm pretty new to CI but am loving it so far.
If there are multiple apps running from the same codebase, is it possible to have a global Database setting (host, username, password) instead of putting them into a config file for each app?

I know this would not be used by many people but I can see uses where a DB server has to be changed, eg moving from Staging to Production. For the moment I have made a change to the DB_driver file but this would be overwritten in an upgrade.

Thanks
#2

[eluser]tonanbarbarian[/eluser]
what you are talking about would probably be useful for more than just the database config.
what i personally do is set a constant in the index.php file
i.e.
Code:
define('SITE', 1);
then in the various config files I use a switch statement to create each of the config sections based on the value of the SITE constant.
#3

[eluser]Elliot Haughin[/eluser]
In terms of the 'singe' config file... that's a simple probem to solve.

In all of your 'individual' config files, just put something like:

Code:
include('/home/user/etc/etc/config.php');

Using a full path, to hit the same config file. Then, in your single config file (at /home/user/etc/etc/config.php) put in the 'normal' contents of a config file.

And the same goes for your db.php, routes.php, pretty much any file that just sets up some local vars like: $config['something'] or $db['something']


Then, to have different database connection based on your environment, try this little gem:

Code:
$development_domain = 'elliot.dev';
$production_domain = 'bigsite.com';

$active_record = TRUE;

$db['development']['hostname'] = "localhost";
$db['development']['username'] = "root";
$db['development']['password'] = "";
$db['development']['database'] = "dev_db";
$db['development']['dbdriver'] = "mysql";
$db['development']['dbprefix'] = "";
$db['development']['pconnect'] = TRUE;
$db['development']['db_debug'] = FALSE;
$db['development']['cache_on'] = FALSE;
$db['development']['cachedir'] = "";
$db['development']['char_set'] = "utf8";
$db['development']['dbcollat'] = "utf8_general_ci";

$db['production']['hostname'] = "mysql.mydomain.com";
$db['production']['username'] = "locked_down_production_user";
$db['production']['password'] = "verylovelypassword";
$db['production']['database'] = "production_db";
$db['production']['dbdriver'] = "mysql";
$db['production']['dbprefix'] = "";
$db['production']['pconnect'] = TRUE;
$db['production']['db_debug'] = FALSE;
$db['production']['cache_on'] = FALSE;
$db['production']['cachedir'] = "";
$db['production']['char_set'] = "utf8";
$db['production']['dbcollat'] = "utf8_general_ci";

if ($domain = $_SERVER['HTTP_HOST'])
{
    $domain = $_SERVER['SERVER_NAME'];
}

if ( empty($domain) )
{
    $domain = $production_domain;
}

switch ($domain)
{
    case $production_domain;
        $active_group = 'production';
    break;
    
    case $development_domain:
        $active_group = 'development';
    break;
    
    default:
        $active_group = 'production';
    break;
}

For your new 'common' database.php file Smile
#4

[eluser]xwero[/eluser]
[quote author="Elliot Haughin" date="1202968635"]

Code:
if ( empty($domain) )
{
    $domain = $production_domain;
}

switch ($domain)
{
    case $production_domain;
        $active_group = 'production';
    break;
    
    case $development_domain:
        $active_group = 'development';
    break;
    
    default:
        $active_group = 'production';
    break;
}
[/quote]

It looks like a lot of checking for the same thing to me.

Code:
$active_group = ($domain == $development_domain)?'development':'production';
#5

[eluser]Lone[/eluser]
I like the suggestion from tonanbarbarian. We use a similar approach for in the template files for clients sites when it comes to loading the Javascript for Google Analytics - it can be annoying working locally and waiting a second or two to wait for a response from Google!

Code:
<? if(!stristr($_SERVER['HTTP_HOST'],'serverIP/address')) { ?>

<!--google code here-->

<? } ?>


You could use this approach as well to switch the 'site' constant or just place it in the database.php file around each section. eg.

Code:
if(stristr($_SERVER['HTTP_HOST'],'dev_server IP/address'))
{
  $db['default']['hostname'] = "dev_server IP/address";
  $db['default']['username'] = "dev_username";
  $db['default']['password'] = "dev_password";
  $db['default']['database'] = "dev_db";
}
else
{
  $db['default']['hostname'] = "live_serverIP/address";
  $db['default']['username'] = "live_username";
  $db['default']['password'] = "live_password";
  $db['default']['database'] = "live_db";
}
#6

[eluser]Sean Downey[/eluser]
Excellent - thank you for the suggestions - so simple when you know how !

I like Elliot's idea with xwero's ammendments.
#7

[eluser]Lone[/eluser]
Oh my god I so didnt see missed reading Elliot's post - looks like I just copied! Tongue
#8

[eluser]wiredesignz[/eluser]
Copying is the highest form of flattery! Tongue
#9

[eluser]Elliot Haughin[/eluser]
Lone,

Code:
if(stristr($_SERVER['HTTP_HOST'],'dev_server IP/address'))

The only reason I wouldn't use this, is because sometimes some environments don't set up HTTP_HOST or SERVER_NAME... or sometimes one but not the other.
It's quite inconsistent really.

That's why I went for:

Code:
if ($domain = $_SERVER['HTTP_HOST'])
{
    $domain = $_SERVER['SERVER_NAME'];
}

if ( empty($domain) )
{
    $domain = $production_domain;
}

It always ensures you've selected one of your 'possible' domains.
#10

[eluser]DPrevite[/eluser]
Is there a way to switch the default database on the fly?

I have two databases and it loads the first one by default, but I'd like to be able to switch to the second and make it the default database from then on for that session.




Theme © iAndrew 2016 - Forum software by © MyBB