Below is code to connect with multiple databases without disconnecting to prior connected databases. It needed some improvement, but I will do some other day.
class db_class{
private static $db_link = array();
private function __construct(){
//creating object is not allowed
}
static function getConnect($type,$db_detail_array=""){
if(empty(self::$db_link[$type])){
self::$db_link[$type] = mysql_connect($db_detail_array['host_name'],$db_detail_array['user'],$db_detail_array['pass']);
mysql_select_db($db_detail_array['db']);
}
}
static function run_query($query,$type){
self::getConnect($type);
$db_query = mysql_query($query,self::$db_link[$type]);
return($db_query) ;
}
static function fetch_array($query,$type){
$db_query = self::run_query($query,$type);
$results = array();
while($fetch_arr=mysql_fetch_assoc($db_query)){
$results[] = $fetch_arr;
}
return($results);
}
static function num_rows($query,$type){
$db_query = self::run_query($query,$type);
$num_rows = mysql_num_rows($db_query);
return($num_rows);
}
static function disconnect($type){
mysql_close(self::$db_link[$type]);
}
}
Usage-
$db_detail_array['host_name'] = "host1";
$db_detail_array['user'] = "user1";
$db_detail_array['pass'] = "pass1";
$db_detail_array['db'] = "database1";
$i = 1;
$type1 = "connection".$i;//connection1
db_class::getConnect($type,$db_detail_array);
$query = "SELECT * FROM `table1`";
$db_arr1 = db_class::fetch_array($query,$type);
print_r($db_arr1);
$db_detail_array['host_name'] = "host2";
$db_detail_array['user'] = "user2";
$db_detail_array['pass'] = "pass2";
$db_detail_array['db'] = "database2";
$i = 2;
$type2 = "connection".$i;//connection2
db_class::getConnect($type,$db_detail_array);
$query = "SELECT * FROM `table2`";
$db_arr2 = db_class::fetch_array($query,$type);
print_r($db_arr2);
//back to first database,
$type = "connection1";//connection1 is same as above so no need to connect db_class::getConnect
$query = "select something from table";
$db_arr2 = db_class::fetch_array($query,$type);//directly run query.
You can connect to any database, switch back to previous database with connecting again and so on. And at end of script end all connections to save memory by,
db_class::disconnect($type1);
db_class::disconnect($type2);
That's all for now. More coming soon.
class db_class{
private static $db_link = array();
private function __construct(){
//creating object is not allowed
}
static function getConnect($type,$db_detail_array=""){
if(empty(self::$db_link[$type])){
self::$db_link[$type] = mysql_connect($db_detail_array['host_name'],$db_detail_array['user'],$db_detail_array['pass']);
mysql_select_db($db_detail_array['db']);
}
}
static function run_query($query,$type){
self::getConnect($type);
$db_query = mysql_query($query,self::$db_link[$type]);
return($db_query) ;
}
static function fetch_array($query,$type){
$db_query = self::run_query($query,$type);
$results = array();
while($fetch_arr=mysql_fetch_assoc($db_query)){
$results[] = $fetch_arr;
}
return($results);
}
static function num_rows($query,$type){
$db_query = self::run_query($query,$type);
$num_rows = mysql_num_rows($db_query);
return($num_rows);
}
static function disconnect($type){
mysql_close(self::$db_link[$type]);
}
}
Usage-
$db_detail_array['host_name'] = "host1";
$db_detail_array['user'] = "user1";
$db_detail_array['pass'] = "pass1";
$db_detail_array['db'] = "database1";
$i = 1;
$type1 = "connection".$i;//connection1
db_class::getConnect($type,$db_detail_array);
$query = "SELECT * FROM `table1`";
$db_arr1 = db_class::fetch_array($query,$type);
print_r($db_arr1);
$db_detail_array['host_name'] = "host2";
$db_detail_array['user'] = "user2";
$db_detail_array['pass'] = "pass2";
$db_detail_array['db'] = "database2";
$i = 2;
$type2 = "connection".$i;//connection2
db_class::getConnect($type,$db_detail_array);
$query = "SELECT * FROM `table2`";
$db_arr2 = db_class::fetch_array($query,$type);
print_r($db_arr2);
//back to first database,
$type = "connection1";//connection1 is same as above so no need to connect db_class::getConnect
$query = "select something from table";
$db_arr2 = db_class::fetch_array($query,$type);//directly run query.
You can connect to any database, switch back to previous database with connecting again and so on. And at end of script end all connections to save memory by,
db_class::disconnect($type1);
db_class::disconnect($type2);
That's all for now. More coming soon.
No comments:
Post a Comment