I am developing a centralized admin module in an environment with multiple mysql database servers which contains multiple databases. Depending on the data I want to recieve I connect to a the desired mysql server. The admin is used to administer several websites through a central CMS system. The users only need to know where they log in to edit their sites, but the sites can be placed on several different hosts.
My db_connect code
Code: Select all
function db_connect($in_db = 'custommain', $ip = ''){
$db_name = $in_db;
if($db_name == 'custommain'){
$mysql_access1 = mysql_connect(admin_db_host, admin_db_user, admin_db_pass) or die ('Database error!');
mysql_select_db($db_name, $mysql_access1);
return $mysql_access1;
}else {
if($ip != ''){
$arrHosts = $GLOBALS['arrHosts'];
$mysql_access2 = mysql_connect($ip, $arrHosts[$ip]['db_user'], $arrHosts[$ip]['db_pass'], TRUE) or die ('Database error!');
mysql_select_db($db_name, $mysql_access2);
echo mysql_error();
return $mysql_access2;
}else{
$mysql_access3 = mysql_connect(db_host, db_user, db_pass) or die ('Database error!');
mysql_select_db($db_name, $mysql_access3);
echo mysql_error();
return $mysql_access3;
}
}
}
I've also made a wrapper
Code: Select all
function dbCloseOpen($in_db, $ip = ''){
mysql_close($GLOBALS['db']);
unset($GLOBALS['db']);
$GLOBALS['db'] = db_connect($in_db, $ip);
}
I hope some one can tell me what I'm doing wrong here or if this is the wrong approah.