I've been looking for a way to make the database connection easy available directry from any script, and from inside other classes. And since I tend to have different connections based on what action to execute, I had to find a way to easily scale the amount of connection needed.
Here's the solution I come up with.
I created this class:
class database{
protected static $details = array(); // holds all connection(s) details
protected static $con = array(); // holds the actual PDO connection(s)
/**
* addConnection()
* Adds a new connection
* @param string $alias a unique identifier for the connection
* @param string $db the database name
* @param string $un username
* @param string $pw password for the user
* @param string $desc a short description for this connection
*/
public static function addConnection($alias,$db,$un,$pw,$desc){
self::$details[$alias]=array('alias'=>$alias,'db'=>$db,'un'=>$un,'pw'=>$pw, 'desc'=>$desc);
}
/**
* getConnection()
* @return array of a specific connection if a valid alias is provided. Othervise return a multidimensional array of all connections.
*/
public static function getConnections($alias=''){
if(!empty($alias)) return self::$details[$alias];
else return self::$details;
}
/**
* setConnection()
* This function moves the connection to $con[] for easy access both inside and outside of this class
* @param string $alias a unique identifier for the connection
* @param string $connection the string of a new pdo connection
*/
public static function setConnection($alias,$connection){
self::$con[$alias] = $connection;
}
/**
* useConnection()
* @param string $alias a unique identifier for the connection
* @return a specific PDO connection based on the given alias
*/
public static function useConnection($alias){
return self::$con[$alias];
}
}
And this script to set/initiate multiple connections:
if(database::getConnections()){
try{
$dbh_options = array(
PDO::ATTR_EMULATE_PREPARES=>false,
PDO::ATTR_DEFAULT_FETCH_MODE=>PDO::FETCH_ASSOC,
PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES utf8',
PDO::ATTR_ERRMODE=>PDO::ERRMODE_SILENT,
);
foreach(database::getConnections() as $dbh){
# initiate connection
database::setConnection($dbh['alias'],new PDO('mysql:host=localhost;dbname='.$dbh['db'].';charset=utf8',$dbh['un'],$dbh['pw'],$dbh_options));
//
# put the connection to a an array for easy access when coding
$con[$dbh['alias']]=database::useConnection($dbh['alias']);
//
}
} catch(PDOException $e) {
file_put_contents('PDOException.txt',$e->getMessage(), FILE_APPEND);
die('OBS! Something went wrong connecting to your database.<br>Please see PDOException.log for more details.');
}
}
Here's how I add new connections:
database::addConnection('alias','database','username','password','a optional short description');
database::addConnection('alias2','database','username','password',null);
Amd this is how I can use the connection from whitin another class:
$select = $con['alias']->prepare('SELECT...');
$insert = $con['alias2']->prepare('INSERT INTO...');
And from other classes, I just extends the database class.
class sql extends database {
public static function query($id){
$query = self::$con['alias']->prepare('SELECT column FROM table WHERE id = ?');
$query->execute(array($id));
return $query->fetchColumn();
}
}
But I would really like your oppinion on this. If there's any security issues with this setup?