<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Matthew Elliston &#187; Singleton Database Class</title>
	<atom:link href="http://www.matthewelliston.com/tag/singleton-database-class/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.matthewelliston.com</link>
	<description>A site filled with my pictures, thoughts and things I want to remember!</description>
	<lastBuildDate>Wed, 24 Aug 2011 14:03:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>PHP Singleton Database Class</title>
		<link>http://www.matthewelliston.com/php-singleton-database-class/</link>
		<comments>http://www.matthewelliston.com/php-singleton-database-class/#comments</comments>
		<pubDate>Tue, 12 May 2009 21:14:16 +0000</pubDate>
		<dc:creator>Matthew</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Database Class]]></category>
		<category><![CDATA[Singleton Database Class]]></category>

		<guid isPermaLink="false">http://www.matthewelliston.com/?p=67</guid>
		<description><![CDATA[A php singleton database class which allows your database queries to be performed quickly and with very few lines of code.]]></description>
			<content:encoded><![CDATA[<p>Hi this is my first post in the way of development on my blog.</p>
<p>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:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php

$db = Database::getInstance();
$results = $db-&amp;gt;query(&quot;SELECT * FROM test WHERE name = :name&quot;,array(&quot;:name&quot; =&amp;gt; &quot;matthew&quot;));

print_r($results);

?&gt;
</pre>
<p><span id="more-67"></span></p>
<p>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.</p>
<p>Here is the full listing of the class.</p>
<pre class="brush: php">
&lt;?php
/**
* Database access class.
* Used in applications where one point of database access is required
*
* Typical Usage:
* $db = Database::getInstance();
* $results = $db-&gt;query("SELECT * FROM test WHERE name = :name",array(":name" =&gt; "matthew"));
* print_r($results);
*
* @author Matthew Elliston &lt;matt@e-titans.com&gt;
* @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-&gt;connection = new PDO("mysql:dbname=spaceinteriors;host=127.0.0.1","root","");
$this-&gt;connection-&gt;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-&gt;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" =&gt; "foo")
* @return array Containing all the remaining rows in the result set.
*/
public function query($query, $args){
$tokens = explode(" ",$query);
try{
$sth = $this-&gt;connection-&gt;prepare($query);
if(empty($args)){
$sth-&gt;execute();
}
else{
$sth-&gt;execute($args);
}
if($tokens[0] == "SELECT"){
$sth-&gt;setFetchMode(PDO::FETCH_ASSOC);
$results = $sth-&gt;fetchAll();
return $results;
}
} catch (PDOException $e) {
echo 'Query failed: ' . $e-&gt;getMessage();
echo '&lt;br /&gt;Query : ' . $query;
}
return 1;
}

/**
* Returns the last inserted ID
*
* @return int ID of the last inserted row
*/
public function lastInsertId(){
return $this-&gt;connection-&gt;lastInsertId();
}
}
?&gt;
</pre>
<p>Please let me know your thoughts on this and any tips to improve it.</p>
<p>I don&#8217;t claim it to be perfect but it fits what I need, so please be kind!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.matthewelliston.com/php-singleton-database-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

