You can also make your class which handles mysql connection to singleton.
class mysqlController {
private $connection;
private $db;
private static $instance;
private function __construct() {
}
public static function getInstance() {
if(!self::$instance) { // First time this method is called
self::$instance = new mysqlController();
}
return self::$instance;
}
public function openConnection($db_host, $db_user, $db_password, $db_name)
{
if(!$this->connection)
{
$this->connection = mysql_connect($db_host, $db_user, $db_password);
if(!$this->connection)
{
die('Database error: ' . mysql_error());
}
else
{
$this->db = mysql_select_db($db_name, $this->connection);
if(!$this->db)
{
die("Database error: " . mysql_error());
}
}
}
}
}
To use the mysql-connection get the instace with getInstance()-function.
$connection = mysqlController::getInstance();
$connection->openConnection('host', 'user', 'pass', 'database');
$connection->query(.....); // For example
Of course you should also need to create here query function etc.