<?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>Working Beta &#187; singleton</title>
	<atom:link href="http://seeds.workingbeta.com/tag/singleton/feed/" rel="self" type="application/rss+xml" />
	<link>http://seeds.workingbeta.com</link>
	<description>a bounch of nearly working code</description>
	<lastBuildDate>Wed, 10 Feb 2010 21:25:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>class.dbmanager.php</title>
		<link>http://seeds.workingbeta.com/2008/11/18/classdbmanagerphp/</link>
		<comments>http://seeds.workingbeta.com/2008/11/18/classdbmanagerphp/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 15:51:28 +0000</pubDate>
		<dc:creator>gc</dc:creator>
				<category><![CDATA[Mysql]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[dbmanagement]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[singleton]]></category>

		<guid isPermaLink="false">http://seeds.workingbeta.com/?p=19</guid>
		<description><![CDATA[here we go, a simple, very basic class to manage DB connections; I know there are so many around BUT

it&#8217;s very lightweight because it:
makes use of the &#8220;singleton&#8221; pattern, so you can start make a mess with patterns [if you've never do so]
ther&#8217;s soo much room for improvement
since I use it on an almost everyday [...]]]></description>
			<content:encoded><![CDATA[<p>here we go, a simple, very basic class to manage DB connections; I know there are so many around <strong>BUT</strong></p>
<ol>
<li>it&#8217;s very lightweight because it:</li>
<li>makes use of the &#8220;<a href="http://en.wikipedia.org/wiki/Singleton_pattern" target="_blank">singleton</a>&#8221; pattern, so you can start make a mess with <a href="http://en.wikipedia.org/wiki/Design_pattern" target="_blank">patterns</a> [if you've never do so]</li>
<li>ther&#8217;s soo much room for improvement</li>
<li>since I use it on an almost everyday basis anyother of my seeds will use it, so get used to it!</li>
</ol>
<p>When you deal with mysql connections you should keep in mind that every single connection takes its load or system resources, and if you don&#8217;t pay attentio you could easily end up into a &#8216;<em>too many connections</em>&#8216; error and a faulty applications.<br />
The very first solutions would be trying to use persistent connections, and this works very well on the mysql side: every page builds just one connection. The singleton pattern is chosen here because off it has a very low footprint into system resources: this means that you free more resources for the rest of your <span style="text-decoration: line-through;">crappy</span> code.</p>
<p>After creating your <strong>dbmanager</strong> Object, you have to <strong>dbmanager::connect </strong>giving host, user, pwd, db as always. from now on, you will have basically two methods:</p>
<p style="text-align: center;"><strong>dbmanager::assocQuery($sql)</strong> and <strong>dbmanager::sqlQuery($sql)</strong></p>
<p>those two methods just query the database with the given query [there are also some nice counters if you wanna know how many querys ar made for every page you build].<br />
The nice thing about dbmanager::assocQuery($sql) is how the result is returned: an associative array with fieldnames as keys: that&#8217;s really useful when you then have to cycle through your recordset and build tables, analyze data and so on.</p>
<blockquote><p><code><br />
$db=new dbconnection("mysql");<br />
$db-&gt;connect($host,$login,$password,$database);<br />
$recordset=$db-&gt;assocQuery("SELECT * FROM TABLENAME WHERE CONDITIONS...");<br />
foreach($recordset as $record){<br />
&nbsp;&nbsp;&nbsp;&nbsp;// here goes your code<br />
&nbsp;&nbsp;&nbsp;&nbsp;}</code>
</p></blockquote>
<p>very straightforward, isn&#8217;t it???</p>
<p>uh.. the code is <a href="http://www.workingbeta.com/seeds/dbmanager.zip">here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://seeds.workingbeta.com/2008/11/18/classdbmanagerphp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
