Any simpler / better way of making a mysql singlton db class in php?

I usually use lazy initialization for this sort of situation and only have one public method (in this case), with a private constructor to prevent outside instantiation (per the Singleton pattern): class Database { private static $instance; private $conn; private function Database() { // do init stuff require_once('dbconfig. Php'); // contains define('DB_USER', 'webuser'); etc... $this->conn = mysql_connect(DB_HOST, DB_USER, DB_PASS); // do error checking } public static function getInstance() { if(!self::$instance) { self::$instance = new Database(); } return self::$instance; } public static function query($sql) { $instance = self::getInstance(); return mysql_query($sql, $instance->conn); } } Then you can just call $dbHandle = Database::getInstance() anytime you need to use it. Or in this case since a static query method is defined, you can use Database::query("select * from xx;") without having to call any sort of init at all.

I usually use lazy initialization for this sort of situation and only have one public method (in this case), with a private constructor to prevent outside instantiation (per the Singleton pattern): class Database { private static $instance; private $conn; private function Database() { // do init stuff require_once('dbconfig. Php'); // contains define('DB_USER', 'webuser'); etc... $this->conn = mysql_connect(DB_HOST, DB_USER, DB_PASS); // do error checking } public static function getInstance() { if(!self::$instance) { self::$instance = new Database(); } return self::$instance; } public static function query($sql) { $instance = self::getInstance(); return mysql_query($sql, $instance->conn); } } Then you can just call $dbHandle = Database::getInstance() anytime you need to use it. Or in this case since a static query method is defined, you can use Database::query("select * from xx;"); without having to call any sort of init at all.

Like include 'db.class. Php' in index. Php vs admin/index.

Php – Citizen Jan 7 '10 at 15:50 Typically, I'll define a ROOT_DIR or some such variable using dirname() style functions to get the absolute path to the root of my site, and then I'd use something like require_once(ROOT_DIR. '/config/db.config. Php');.

That way it doesn't matter where the current dir is when grabbing the config. – Dan Breen Jan 7 '10 at 21:59 I'm probably asking too much, but when I try to use your class, I get a rabbit hole of errors. Added 'function' to your 'public static' lines and then I get an error for using $this out of object context.

Any ideas? – Citizen Jan 7 '10 at 23:16 Sorry, I didn't actually try to run this, so I missed a few syntax errors. I've edited the post and tested it; it works now.

The $this->instance should have been self::$instance in the public static method. – Dan Breen Jan 7 '10 at 3:18.

That's a simple as it gets, that will work fine. You can pass your credentials to init(); include(config. Php); $oDb = Database::init( DB_HOST, DB_NAME, DB_USER, DB_PASSWORD ); $sql = foo; $oDb->query($sql).

You can use an include inside a function inside a class or you can just pass the variables... or you can store the credentials in a php. Ini file php. Ini file: mysql.

Default_host="host" mysql. Default_user="user" mysql. Default_password="password.

For singleton classes, the model that Dan Breen followed is cleanest and very common. However, in this case, I would also allow the getInstance method to accept some parameters so that you can override your default configuration at instantiation time, or just get a reference without creating a connection (both use-cases happen from time to time). Database.

Php require_once("../path/to/config/database. Php"); class Database { private static $instances = array(); private function Database($host, $user, $password, $name) { // do init stuff } public static getInstance( $host=DB_HOST, $user=DB_USER, $password=DB_PASSWORD, $name=DB_NAME ) { $key = strtolower($host . $user .

$password . $name); if (!$self::instances$key ) { $self::instances$key = new Database($host, $user, $password, $name); } return $self::instances$key; } } ..config/database. Php: define("DB_HOST", "localhost"); define("DB_USER", "mrsqlguy"); define("DB_PASS", "!"); define("DB_NAME", "just_another_wordpress"); Edit: I've changed it to act more like a flyweight to ensure that you only get one instance for each connect location/database.

This addresses your concerns and maintains a degree of flexibility.

Being flexible is nice and I like this answer, but a potential problem is that since it's only initialized once, it could lead to very strange problems later. Namely, if the programmer is calling getInstance with some new connection parameters but somewhere else it already made the initial call with different ones, it could be difficult to figure out that it's not using the parameters passed in the second time. – Dan Breen Jan 7 '10 at 6:10.

Debug_log_path' => '/tmp/mysql.debug. 'query_log_path' => '/tmp/mysql.debug. $this->handle_error('#12A','invalid field type.

$this->handle_error('#13A','invalid field type.

I usually use lazy initialization for this sort of situation and only have one public method (in this case), with a private constructor to prevent outside instantiation (per the Singleton pattern).

That's a simple as it gets, that will work fine.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions