PHP Singleton Database Class
Hi this is my first post in the way of development on my blog.
I have decided to upload my database class that I use for projects. Its by no means perfect. There is quite a heated debate on Singleton classes themselves. However I like how it works for me. I can query query a database quicklyby simply using:
<?php
$db = Database::getInstance();
$results = $db->query("SELECT * FROM test WHERE name = :name",array(":name" => "matthew"));
print_r($results);
?>
The function query can be passed an array of arguments that can be used when wanting to sanitize the data before querying the database. This uses the standard PDO::prepare() function to create the prepared statement. It can accept the :foo or ? format of statement.
Here is the full listing of the class.
<?php
/**
* Database access class.
* Used in applications where one point of database access is required
*
* Typical Usage:
* $db = Database::getInstance();
* $results = $db->query("SELECT * FROM test WHERE name = :name",array(":name" => "matthew"));
* print_r($results);
*
* @author Matthew Elliston <matt@e-titans.com>
* @version 1.0
*/
class Database {
/**
* Instance of the database class
* @static Database $instance
*/
private static $instance;
/**
* Database connection
* @access private
* @var PDO $connection
*/
private $connection;
/**
* Constructor
* @param $dsn The Data Source Name. eg, "mysql:dbname=testdb;host=127.0.0.1"
* @param $username
* @param $password
*/
private function __construct(){
$this->connection = new PDO("mysql:dbname=spaceinteriors;host=127.0.0.1","root","");
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
/**
* Gets an instance of the Database class
*
* @static
* @return Database An instance of the database singleton class.
*/
public static function getInstance(){
if(empty(self::$instance)){
try{
self::$instance = new Database();
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
}
return self::$instance;
}
/**
* Runs a query using the current connection to the database.
*
* @param string query
* @param array $args An array of arguments for the sanitization such as array(":name" => "foo")
* @return array Containing all the remaining rows in the result set.
*/
public function query($query, $args){
$tokens = explode(" ",$query);
try{
$sth = $this->connection->prepare($query);
if(empty($args)){
$sth->execute();
}
else{
$sth->execute($args);
}
if($tokens[0] == "SELECT"){
$sth->setFetchMode(PDO::FETCH_ASSOC);
$results = $sth->fetchAll();
return $results;
}
} catch (PDOException $e) {
echo 'Query failed: ' . $e->getMessage();
echo '<br />Query : ' . $query;
}
return 1;
}
/**
* Returns the last inserted ID
*
* @return int ID of the last inserted row
*/
public function lastInsertId(){
return $this->connection->lastInsertId();
}
}
?>
Please let me know your thoughts on this and any tips to improve it.
I don’t claim it to be perfect but it fits what I need, so please be kind!