Configure HTML/JavaScript

Thursday, December 9, 2010

managing multiple Databases in php

Hi,
This is important to manage your database connections in php if you are using many connections in a single application. you can use single ton pattern in php to make is clear.

Exe:
class Connector {
    private $conn;
    private $intranetdb = 'jprod';
    private $metricsdb = 'metrics_prod';
    private function __costructor(){
           
    }
    static function intranetDb(){
        return 'jprod';
    }
    static function metricsDb(){
        return 'metrics_prod';
    }
    static function makeConnection($host, $username, $password){
        return $this->conn = mysql_connect($host, $username, $password);
    }
    static function prodConnection(){
       
        $prod_dbhost         = 'localhost1';
        $prod_dbuser         = 'jroot';
        $prod_dbpass         = 'joomla1';
        /*
        $prod_dbhost         = 'localhost';
        $prod_dbuser         = 'root';
        $prod_dbpass         = '';
        */
        $prod_conn = mysql_connect($prod_dbhost, $prod_dbuser, $prod_dbpass) or die ('Error connecting to mysql');
        return $prod_conn;
    }
    static function metricsConnection(){
       
        $dbhost         = 'localhost2';
        $dbuser         = 'jroot';
        $dbpass         = 'joomla1';
        /*
        $dbhost         = 'localhost';
        $dbuser         = 'root';
        $dbpass         = '';
        */
        $db             = 'metrics_prod';
        $conn = mysql_connect($dbhost, $dbuser, $dbpass);
        return $conn;
    }
    static function closeConnection($resource){
            return mysql_close($resource);
    }
    static function freeResult($result){
            return mysql_free_result($result);
    }
}

No comments:

Post a Comment