Dynamically choosing databased based on http host (Is this ok>) - El Forum - 05-14-2011
[eluser]blasto333[/eluser]
I have CodeIgnitor setup to pick the database based on the subdomain it is loaded in (as shown below).
I turned of persistent connections as I use temp tables and different databases. It seems to be working fine, but I was just wondering if there are any potential problems. (I also use database backed sessions)
Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$active_group = "default";
$active_record = TRUE;
$phppos_client_name = substr($_SERVER['HTTP_HOST'], 0, strpos($_SERVER['HTTP_HOST'], '.'));
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "USER";
$db['default']['password'] = "PASSWORD";
$db['default']['database'] = "db_$phppos_client_name";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "phppos_";
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
/* End of file database.php */
/* Location: ./application/config/database.php */
Dynamically choosing databased based on http host (Is this ok>) - El Forum - 05-17-2011
[eluser]toopay[/eluser]
Its better to have two or more configuration, like
Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$active_group = "default";
$active_record = TRUE;
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "USER1";
$db['default']['password'] = "PASSWORD1";
$db['default']['database'] = "db_default";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "phppos_";
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
// Other database configuration
$active_group = "othersetting";
$active_record = TRUE;
$db['othersetting']['hostname'] = "localhost";
$db['othersetting']['username'] = "USER2";
$db['othersetting']['password'] = "PASSWORD2";
$db['othersetting']['database'] = "db_other";
$db['othersetting']['dbdriver'] = "mysql";
$db['othersetting']['dbprefix'] = "phppos_";
$db['othersetting']['pconnect'] = FALSE;
$db['othersetting']['db_debug'] = FALSE;
$db['othersetting']['cache_on'] = FALSE;
$db['othersetting']['cachedir'] = "";
$db['othersetting']['char_set'] = "utf8";
$db['othersetting']['dbcollat'] = "utf8_general_ci";
Then, you can create some "switching" helper( or library if necessary) , to load each database based by SERVER value like above. To load each config group, you can do it with :
Code: if(...)
{
$this->load->database('othersetting');
}
else
{
// This will load the 'default' group
$this->load->database();
}
|