I am using pdo and I have set the connection string in a config file such as
$db = new PDO(\"mysql:host=localhost;dbname=mydbname\", \'root\', \'pass\');
I recommend to use a good pattern called Registry. And a simple implementation in PHP:
abstract class Registry {
private static $_tools = array();
public static function set($name, $value) {
self::$_tools[$name] = $value;
}
public static function get($name) {
return (isset(self::$_tools[$name]) ? self::$_tools[$name] : null);
}
}
Usage:
$db = new PDO("mysql:host=localhost;dbname=mydbname", 'root', 'pass');
Registry::set('db', $db);
//In some other part of code
$query = Registry::get('db')->query("select aUsername,aPassword,aOnline,aLastlogin from tbl_admins where aUsername = '$username'");