<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/rss2full.xsl" type="text/xsl" media="screen"?><?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/itemcontent.css" type="text/css" media="screen"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Justin Carmony's Blog</title>
	
	<link>http://www.justincarmony.com/blog</link>
	<description>Web Designer &amp; Software Engineer</description>
	<pubDate>Fri, 21 Nov 2008 15:46:56 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/JustinCarmony" type="application/rss+xml" /><item>
		<title>Streamlined Web Development: In-Depth</title>
		<link>http://feeds.feedburner.com/~r/JustinCarmony/~3/459783094/</link>
		<comments>http://www.justincarmony.com/blog/2008/11/20/streamlined-web-development-in-depth/#comments</comments>
		<pubDate>Thu, 20 Nov 2008 16:26:44 +0000</pubDate>
		<dc:creator>Justin Carmony</dc:creator>
		
		<category><![CDATA[General]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Technology]]></category>

		<category><![CDATA[Web Design]]></category>

		<guid isPermaLink="false">http://www.justincarmony.com/blog/?p=285</guid>
		<description><![CDATA[Note: This is a work in progress. I will finish the rest after the presentation. I just wanted to publish the post to link to. If you have any questions, please post them below I will try to answer them as fast as I can. I probably won&#8217;t have time to finish this until Sunday, [...]


Related posts:<ol><li><a href='http://www.justincarmony.com/blog/2008/11/13/speaking-utah-php-usergroup-streamlined-web-development/' rel='bookmark' title='Permanent Link: Speaking: Utah PHP Usergroup - Streamlined Web Development'>Speaking: Utah PHP Usergroup - Streamlined Web Development</a> <small>Next Thursday, November 20th, at 7 PM Mountain Time (9PM...</small></li><li><a href='http://www.justincarmony.com/blog/2008/07/15/web-development-10-years-ago-now/' rel='bookmark' title='Permanent Link: Web Development 10-Years Ago &#038; Now'>Web Development 10-Years Ago &#038; Now</a> <small>This week's blogging challenge was our computer software now and...</small></li><li><a href='http://www.justincarmony.com/blog/2008/09/24/zend-studio-vs-php-development-tools/' rel='bookmark' title='Permanent Link: Zend Studio vs PHP Development Tools'>Zend Studio vs PHP Development Tools</a> <small> I've been using Zend Studio (ZS) for the last...</small></li></ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p><em>Note: This is a work in progress. I will finish the rest after the presentation. I just wanted to publish the post to link to. If you have any questions, please post them below I will try to answer them as fast as I can. I probably won&#8217;t have time to finish this until Sunday, due to several critical projects that are due ASAP.<br />
</em></p>
<p>This in the &#8220;In-Depth&#8221; part of my presentation on &#8220;Streamling Web Development with SVN &amp; Trac.&#8221; I can&#8217;t possibly cover all the aspects of this topic. I like to post a &#8220;in-depth&#8221; article right after my presentation with links, tutorials, and all my resources used for the presentation. This allows for me to spend more time on questions &amp; answers. I&#8217;d appreciate any feedback on the presentation, and if you have any questions feel free to ask. This article is an extension to the presentation, so I suggest listening to the presentation first if it seems like there are any pieces missing.</p>
<h2>Presentation</h2>
<p>Here is the Scribd version of my Presentation. Since it didn&#8217;t like some of the things I used in the presentation, I uploaded a <a href="http://www.scribd.com/doc/8210453/UPHPU-Streamlined-Web-Development-PDF" target="_blank">pdf version</a> as well.</p>
<p><a style="margin: 12px auto 6px auto; font-family: Helvetica,Arial,Sans-serif; font-style: normal; font-variant: normal; font-weight: normal; font-size: 14px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; display: block; text-decoration: underline;" title="View UPHPU - Streamlined Web Development document on Scribd" href="http://www.scribd.com/doc/8210331/UPHPU-Streamlined-Web-Development">UPHPU - Streamlined Web Development</a> <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="100%" height="500" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="id" value="doc_254931738339357" /><param name="name" value="doc_254931738339357" /><param name="align" value="middle" /><param name="quality" value="high" /><param name="play" value="true" /><param name="loop" value="true" /><param name="scale" value="showall" /><param name="wmode" value="opaque" /><param name="devicefont" value="false" /><param name="bgcolor" value="#ffffff" /><param name="menu" value="true" /><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="salign" /><param name="src" value="http://documents.scribd.com/ScribdViewer.swf?document_id=8210331&amp;access_key=key-1qeaelekdzjywig9fmfq&amp;page=1&amp;version=1&amp;viewMode=" /><embed id="doc_254931738339357" type="application/x-shockwave-flash" width="100%" height="500" src="http://documents.scribd.com/ScribdViewer.swf?document_id=8210331&amp;access_key=key-1qeaelekdzjywig9fmfq&amp;page=1&amp;version=1&amp;viewMode=" allowscriptaccess="always" allowfullscreen="true" menu="true" bgcolor="#ffffff" devicefont="false" wmode="opaque" scale="showall" loop="true" play="true" quality="high" align="middle" name="doc_254931738339357"></embed></object></p>
<div style="margin: 6px auto 3px auto; font-family: Helvetica,Arial,Sans-serif; font-style: normal; font-variant: normal; font-weight: normal; font-size: 12px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; display: block;"><a style="text-decoration: underline;" href="http://www.scribd.com/upload">Get your own</a> at Scribd or <a style="text-decoration: underline;" href="http://www.scribd.com/browse">explore</a> others:		  <a style="text-decoration: underline;" href="http://www.scribd.com/browse?c=114-technology">Technology</a> <a style="text-decoration: underline;" href="http://www.scribd.com/browse?c=120-web-2-0">Web 2.0</a> <a style="text-decoration: underline;" href="http://www.scribd.com/tag/websites">websites</a> <a style="text-decoration: underline;" href="http://www.scribd.com/tag/ajax">ajax</a></div>
<h2>Overview</h2>
<p>As a web developer, I like to spend the most time possible on adding improvements, making changes, and ensuring quality. What I don&#8217;t like spending time on is repetitive tasks trying to manage the project. Over the last few years, I&#8217;ve been trying to focus on &#8220;automating&#8221; the processes of web development, letting programs, scripts, and othe technologies keep me focused on real development. I&#8217;ve pooled together all the information I&#8217;ve found on the subject, and hopefully I can help anyone searching for more information.<span id="more-285"></span></p>
<h2>The Development Cycle</h2>
<p>There is one constant in any type of software development: change. Every project is going to have new features, bug fixes, feature changes, etc. Our goal is to help create, verify, and deploy these changes as quickly as possible, while ensuring quality. There are five basic steps, in my opinion, to this cycle:</p>
<p><a href="http://www.justincarmony.com/blog/wp-content/uploads/2008/11/development-cycle-diagram.jpg"><img class="alignnone size-medium wp-image-290" title="development-cycle-diagram" src="http://www.justincarmony.com/blog/wp-content/uploads/2008/11/development-cycle-diagram-300x223.jpg" alt="" width="300" height="223" /></a></p>
<ul>
<li>Code Changes</li>
<li>Test / QA Changes</li>
<li>Stage Changes</li>
<li>Deploy Changes (Push Changes Live)</li>
<li>Feedback</li>
</ul>
<h3>Code Changes</h3>
<p>This is where developers get down and get their hands dirty. To be honest, this is my favorite part of the process, as I would think most developers would agree. However, if we only focus on this aspect, our products will suffer from bugs and errors.</p>
<p>I use SVN to manage all of my code and my changes. &#8220;Subversion (SVN) &#8230; is used to maintain current and historical versions of files such as <a title="Source code" href="http://en.wikipedia.org/wiki/Source_code">source code</a>, web pages, and documentation.&#8221; (<a href="http://en.wikipedia.org/wiki/Subversion_(software)" target="_blank">Wikipedia</a>) Basically, it is a great tool to manage your source code, whether it is PHP, HTML, CSS, JS, Images, etc. If you are unfamiliar with SVN and source control concepts, I highly recommend visiting the <a href="http://svnbook.red-bean.com/">online SVN Book</a>. The chapters on <a href="http://svnbook.red-bean.com/en/1.5/svn-book.html#svn.basic" target="_blank">Fundimental Concepts</a> and <a href="http://svnbook.red-bean.com/en/1.5/svn-book.html#svn.tour" target="_blank">Basic Usage</a> are great primers for SVN. To give a tutorial on SVN is outside the scope of this article, so I would suggest reading &amp; experimenting with SVN before trying to continue this article.</p>
<p>Important steps to working with SVN is that you must be able to run your website code locally. Some beginners (myself included when I started), use SVN like a glorified FTP, using it to upload their files to the server, and &#8220;commit&#8221; hundreds of tweaks and changes in a short period. Soon you will have thousands of revisions that aren&#8217;t meaningful. Running your code locally, by either installing your enviroment or using a local server (physical or virtual), you can make a single &#8220;change&#8221; that you&#8217;ve tested &amp; ensured before &#8220;commiting&#8221; it to your SVN repository. This way you review your SVN repository history, each revision is meaningful. With good commit comments, you will see &#8220;Added extra field for middle name on registration&#8221; in the history, and all code changes needed for that one &#8220;feature change&#8221; will be in a single revision entry.</p>
<p>There are many tutorials for running your code locally. If you want to install Apache, MySQL, and PHP on your machine, I suggest using <a href="http://www.apachefriends.org/en/xampp.html" target="_blank">XAMPP</a>. It works with Windows, Linux, Mac OSX, and others. I have a <a href="http://www.justincarmony.com/blog/2008/10/14/php-video-tutorial-getting-started-installing-xampp/" target="_blank">video tutorial for installing XAMPP on windows</a>.</p>
<p>If you want to investigate using Virtual Machines for a local test environment, I would suggest checking out <a href="http://www.virtualbox.org/" target="_blank">Sun&#8217;s VirtualBox</a>. It is Open Source (GPL) and I like to use it when I don&#8217;t want to pay for the expensive VMWare solutions. There is <a href="http://kevin.vanzonneveld.net/techblog/article/how_virtualization_will_improve_your_code/" target="_blank">a great article on using a Virtual Ubuntu Server</a> as a local development enviroment. This tutorial is based on using VirtualBox to virtualize Ubuntu within Ubuntu. Cal Evans takes the same approach, but you can read how he tried <a href="http://blog.calevans.com/2008/10/08/virtualbox-and-php-development/" target="_blank">doing it using a windows environment</a>.</p>
<h3>Testing / QA Changes</h3>
<p>Once you&#8217;ve coded and tested your changes locally, you commit your changes to the SVN repository. Using a post-commit hook with SVN, we update</p>
<h2>General Setup</h2>
<p>Here is a diagram showing the basic setup for streamlining a project with LAMP, SVN, and Trac.</p>
<p><a href="http://www.justincarmony.com/blog/wp-content/uploads/2008/11/svn-trac-diagram.jpg"><img class="alignnone size-medium wp-image-289" title="svn-trac-diagram" src="http://www.justincarmony.com/blog/wp-content/uploads/2008/11/svn-trac-diagram-300x203.jpg" alt="" width="300" height="203" /></a></p>


<p>Related posts:<ol><li><a href='http://www.justincarmony.com/blog/2008/11/13/speaking-utah-php-usergroup-streamlined-web-development/' rel='bookmark' title='Permanent Link: Speaking: Utah PHP Usergroup - Streamlined Web Development'>Speaking: Utah PHP Usergroup - Streamlined Web Development</a> <small>Next Thursday, November 20th, at 7 PM Mountain Time (9PM...</small></li><li><a href='http://www.justincarmony.com/blog/2008/07/15/web-development-10-years-ago-now/' rel='bookmark' title='Permanent Link: Web Development 10-Years Ago &#038; Now'>Web Development 10-Years Ago &#038; Now</a> <small>This week's blogging challenge was our computer software now and...</small></li><li><a href='http://www.justincarmony.com/blog/2008/09/24/zend-studio-vs-php-development-tools/' rel='bookmark' title='Permanent Link: Zend Studio vs PHP Development Tools'>Zend Studio vs PHP Development Tools</a> <small> I've been using Zend Studio (ZS) for the last...</small></li></ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p><img src="http://feeds.feedburner.com/~r/JustinCarmony/~4/459783094" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.justincarmony.com/blog/2008/11/20/streamlined-web-development-in-depth/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.justincarmony.com/blog/2008/11/20/streamlined-web-development-in-depth/</feedburner:origLink></item>
		<item>
		<title>Speaking: Utah PHP Usergroup - Streamlined Web Development</title>
		<link>http://feeds.feedburner.com/~r/JustinCarmony/~3/452055781/</link>
		<comments>http://www.justincarmony.com/blog/2008/11/13/speaking-utah-php-usergroup-streamlined-web-development/#comments</comments>
		<pubDate>Thu, 13 Nov 2008 17:41:51 +0000</pubDate>
		<dc:creator>Justin Carmony</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Web Design]]></category>

		<category><![CDATA[event]]></category>

		<category><![CDATA[git]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[svn]]></category>

		<category><![CDATA[trac]]></category>

		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.justincarmony.com/blog/?p=283</guid>
		<description><![CDATA[Next Thursday, November 20th, at 7 PM Mountain Time (9PM EST, 6PM PST) I will be speaking at the Utah PHP Usergroup meeting on &#8220;Streamlined Web Development.&#8221; I will be speaking with Kevin Carter from Code Greene. Here is the annoucement on the UPHPU website:
I’m very pleased to announce the topic for November’s UPHPU Meeting [...]


Related posts:<ol><li><a href='http://www.justincarmony.com/blog/2008/11/20/streamlined-web-development-in-depth/' rel='bookmark' title='Permanent Link: Streamlined Web Development: In-Depth'>Streamlined Web Development: In-Depth</a> <small>Note: This is a work in progress. I will finish...</small></li><li><a href='http://www.justincarmony.com/blog/2008/09/24/zend-studio-vs-php-development-tools/' rel='bookmark' title='Permanent Link: Zend Studio vs PHP Development Tools'>Zend Studio vs PHP Development Tools</a> <small> I've been using Zend Studio (ZS) for the last...</small></li><li><a href='http://www.justincarmony.com/blog/2008/07/15/web-development-10-years-ago-now/' rel='bookmark' title='Permanent Link: Web Development 10-Years Ago &#038; Now'>Web Development 10-Years Ago &#038; Now</a> <small>This week's blogging challenge was our computer software now and...</small></li></ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>Next Thursday, November 20th, at 7 PM Mountain Time (9PM EST, 6PM PST) I will be speaking at the Utah PHP Usergroup meeting on &#8220;Streamlined Web Development.&#8221; I will be speaking with Kevin Carter from <a href="http://www.codegreene.com/" target="_blank">Code Greene</a>. Here is the annoucement on the <a href="http://uphpu.org" target="_blank">UPHPU website</a>:</p>
<blockquote><p>I’m very pleased to announce the topic for November’s UPHPU Meeting : Streamlined Web Development using a VCS (Version Control System) and Trac.</p>
<p>Starting the meeting off will be Code Greene’s Kevin Carter. Kevin, who has used Git in a production environment for several months now, will be presenting a primer on how to get started with Git. Additionally, he’ll cover the advantages of distributed source code control and possibly some topics on using git in an svn eniroment. His blog can be found at http://dexterthedragon.com.</p>
<p>Following Kevin will be are main speaker, Justin Carmony. His presentation will focus on the unique challenges with developing, deploying, &amp; managing Website development. Justin Carmony has put together his trips, tricks, and suggestions for streamlining the development cycle. With a focus on automation, using SVN and Trac allows developers to focus on development rather than redundant tasks. Whether a small project with a single developer, or a large project with a big team, SVN and Trac in a LAMP environment can help any developer. Time permitting he will show some of his own live examples of using SVN &amp; Trac. If you have a specific topics or questions you would like addressed in his presentation, please feel free to let him know on his blog.</p>
<p>Justin Carmony has worked in web development professionally for the last four years. With emphasis on PHP, .NET, and Web Services, he has worked on projects ranging from simple websites to complex communications between thousands of remote systems. He currently is working as an independent contractor &amp; private consultant. If you have any questions, comments, or curiosities you can contact him (justin AT justincarmony DOT com) or read about his latest endeavors at his blog (www.justincarmony.com/blog).</p>
<p>We will have streaming as usual and slides will be posted early.</p></blockquote>
<p>If you&#8217;re in the Salt Lake City area, and would like to attend, it will be at the following address: 12393 Gateway Park Place Suite 600, Draper, Utah.</p>
<p>If you&#8217;re not able to make it, but would still like to listen in &amp; participate via IRC, check out the <a href="http://uphpu.org/live-meeting/" target="_blank">UPHPU&#8217;s live meeting page</a>. If you have any specifics on what you would like to hear, or have any questions that you would like me to answer in my presentation, leave a comment below.</p>


<p>Related posts:<ol><li><a href='http://www.justincarmony.com/blog/2008/11/20/streamlined-web-development-in-depth/' rel='bookmark' title='Permanent Link: Streamlined Web Development: In-Depth'>Streamlined Web Development: In-Depth</a> <small>Note: This is a work in progress. I will finish...</small></li><li><a href='http://www.justincarmony.com/blog/2008/09/24/zend-studio-vs-php-development-tools/' rel='bookmark' title='Permanent Link: Zend Studio vs PHP Development Tools'>Zend Studio vs PHP Development Tools</a> <small> I've been using Zend Studio (ZS) for the last...</small></li><li><a href='http://www.justincarmony.com/blog/2008/07/15/web-development-10-years-ago-now/' rel='bookmark' title='Permanent Link: Web Development 10-Years Ago &#038; Now'>Web Development 10-Years Ago &#038; Now</a> <small>This week's blogging challenge was our computer software now and...</small></li></ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p><img src="http://feeds.feedburner.com/~r/JustinCarmony/~4/452055781" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.justincarmony.com/blog/2008/11/13/speaking-utah-php-usergroup-streamlined-web-development/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.justincarmony.com/blog/2008/11/13/speaking-utah-php-usergroup-streamlined-web-development/</feedburner:origLink></item>
		<item>
		<title>Digital Conversion Video</title>
		<link>http://feeds.feedburner.com/~r/JustinCarmony/~3/444942465/</link>
		<comments>http://www.justincarmony.com/blog/2008/11/06/digital-conversion-video/#comments</comments>
		<pubDate>Fri, 07 Nov 2008 00:29:40 +0000</pubDate>
		<dc:creator>Justin Carmony</dc:creator>
		
		<category><![CDATA[General]]></category>

		<category><![CDATA[Funny]]></category>

		<guid isPermaLink="false">http://www.justincarmony.com/blog/2008/11/06/digital-conversion-video/</guid>
		<description><![CDATA[This made my day, and I thought I would share it.



Related posts:It&#8217;s Time for Campaignin&#8217; - Elections Video I couldn't help resist posting this video I found over...PHP Video Tutorial - Getting Started - Installing XAMPP  I've decided to give this a try and make...
Related posts brought to you by Yet Another Related Posts [...]


Related posts:<ol><li><a href='http://www.justincarmony.com/blog/2008/10/07/its-time-for-campaignin-elections-video/' rel='bookmark' title='Permanent Link: It&#8217;s Time for Campaignin&#8217; - Elections Video'>It&#8217;s Time for Campaignin&#8217; - Elections Video</a> <small>I couldn't help resist posting this video I found over...</small></li><li><a href='http://www.justincarmony.com/blog/2008/10/14/php-video-tutorial-getting-started-installing-xampp/' rel='bookmark' title='Permanent Link: PHP Video Tutorial - Getting Started - Installing XAMPP'>PHP Video Tutorial - Getting Started - Installing XAMPP</a> <small> I've decided to give this a try and make...</small></li></ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>This made my day, and I thought I would share it.</p>
<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/iTSS8E7bKXg&#038;hl=en&#038;fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/iTSS8E7bKXg&#038;hl=en&#038;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>


<p>Related posts:<ol><li><a href='http://www.justincarmony.com/blog/2008/10/07/its-time-for-campaignin-elections-video/' rel='bookmark' title='Permanent Link: It&#8217;s Time for Campaignin&#8217; - Elections Video'>It&#8217;s Time for Campaignin&#8217; - Elections Video</a> <small>I couldn't help resist posting this video I found over...</small></li><li><a href='http://www.justincarmony.com/blog/2008/10/14/php-video-tutorial-getting-started-installing-xampp/' rel='bookmark' title='Permanent Link: PHP Video Tutorial - Getting Started - Installing XAMPP'>PHP Video Tutorial - Getting Started - Installing XAMPP</a> <small> I've decided to give this a try and make...</small></li></ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p><img src="http://feeds.feedburner.com/~r/JustinCarmony/~4/444942465" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.justincarmony.com/blog/2008/11/06/digital-conversion-video/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.justincarmony.com/blog/2008/11/06/digital-conversion-video/</feedburner:origLink></item>
		<item>
		<title>MySQL - Does Table Exist w/o Throwing Errors</title>
		<link>http://feeds.feedburner.com/~r/JustinCarmony/~3/441563698/</link>
		<comments>http://www.justincarmony.com/blog/2008/11/03/mysql-does-table-exist-wo-throwing-errors/#comments</comments>
		<pubDate>Tue, 04 Nov 2008 00:34:33 +0000</pubDate>
		<dc:creator>Justin Carmony</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[MySQL]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Tips and Tricks]]></category>

		<guid isPermaLink="false">http://www.justincarmony.com/blog/?p=280</guid>
		<description><![CDATA[There are times where you would like to know if a table exists before executing an query. Most solutions require having MySQL throw an error saying "table does not exist," but I prefer a cleaner way. I found on this forum post a clean way to do it:
PLAIN TEXT
SQL:




/* example with table name: table_name */


SHOW [...]


Related posts:<ol><li><a href='http://www.justincarmony.com/blog/2008/07/01/mysql-php-sql_calc_found_rows-an-easy-way-to-get-the-total-number-of-rows-regardless-of-limit/' rel='bookmark' title='Permanent Link: MySQL &#038; PHP  - SQL_CALC_FOUND_ROWS - An easy way to get the total number of rows regardless of LIMIT'>MySQL &#038; PHP  - SQL_CALC_FOUND_ROWS - An easy way to get the total number of rows regardless of LIMIT</a> <small>Here is a little trick I found awhile back. I...</small></li><li><a href='http://www.justincarmony.com/blog/2008/02/19/ms-sql-2005-t-sql-row-count-for-each-table/' rel='bookmark' title='Permanent Link: MS SQL 2005 (T-SQL) Row Count for Each Table'>MS SQL 2005 (T-SQL) Row Count for Each Table</a> <small>At work I needed a solution to give me a...</small></li><li><a href='http://www.justincarmony.com/blog/2008/02/25/asp-net-linq-gridviews-and-guid-errors/' rel='bookmark' title='Permanent Link: ASP .NET, LINQ, GridViews, and GUID Errors'>ASP .NET, LINQ, GridViews, and GUID Errors</a> <small>I found a simple problem today. Here is the deal,...</small></li></ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>There are times where you would like to know if a table exists before executing an query. Most solutions require having MySQL throw an error saying "table does not exist," but I prefer a cleaner way. I found on this forum post a clean way to do it:</p>
<div class="igBar"><span id="lsql-11"><a href="#" onclick="javascript:showPlainTxt('sql-11'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">SQL:</span>
<div id="sql-11">
<div class="sql">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">/* example with table name: table_name */</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">SHOW</span> <span style="color: #993333; font-weight: bold;">TABLES</span> <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'table_name'</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This solution will return 1 row if it exists, and 0 rows if it doesn't. Here is a PHP example using this:</p>
<div class="igBar"><span id="lphp-12"><a href="#" onclick="javascript:showPlainTxt('php-12'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-12">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">function</span> DoesTableExist<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$name</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color:#0000FF;">$sql</span> = <span style="color:#FF0000;">"SHOW TABLES LIKE '$name'"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color:#0000FF;">$result</span> = <a href="http://www.php.net/mysql_query"><span style="color:#000066;">mysql_query</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$sql</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/mysql_num_rows"><span style="color:#000066;">mysql_num_rows</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$result</span><span style="color:#006600; font-weight:bold;">&#41;</span>&gt; <span style="color:#CC66CC;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">true</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color:#616100;">else</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">false</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span>DoesTableExist<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'users'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"Users table found!"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">else</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"Users NOT FOUND!!!"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>


<p>Related posts:<ol><li><a href='http://www.justincarmony.com/blog/2008/07/01/mysql-php-sql_calc_found_rows-an-easy-way-to-get-the-total-number-of-rows-regardless-of-limit/' rel='bookmark' title='Permanent Link: MySQL &#038; PHP  - SQL_CALC_FOUND_ROWS - An easy way to get the total number of rows regardless of LIMIT'>MySQL &#038; PHP  - SQL_CALC_FOUND_ROWS - An easy way to get the total number of rows regardless of LIMIT</a> <small>Here is a little trick I found awhile back. I...</small></li><li><a href='http://www.justincarmony.com/blog/2008/02/19/ms-sql-2005-t-sql-row-count-for-each-table/' rel='bookmark' title='Permanent Link: MS SQL 2005 (T-SQL) Row Count for Each Table'>MS SQL 2005 (T-SQL) Row Count for Each Table</a> <small>At work I needed a solution to give me a...</small></li><li><a href='http://www.justincarmony.com/blog/2008/02/25/asp-net-linq-gridviews-and-guid-errors/' rel='bookmark' title='Permanent Link: ASP .NET, LINQ, GridViews, and GUID Errors'>ASP .NET, LINQ, GridViews, and GUID Errors</a> <small>I found a simple problem today. Here is the deal,...</small></li></ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p><img src="http://feeds.feedburner.com/~r/JustinCarmony/~4/441563698" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.justincarmony.com/blog/2008/11/03/mysql-does-table-exist-wo-throwing-errors/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.justincarmony.com/blog/2008/11/03/mysql-does-table-exist-wo-throwing-errors/</feedburner:origLink></item>
		<item>
		<title>I’m Boycotting Electronic Arts (EA) Games</title>
		<link>http://feeds.feedburner.com/~r/JustinCarmony/~3/437704111/</link>
		<comments>http://www.justincarmony.com/blog/2008/10/30/im-boycotting-electronic-arts-ea-games/#comments</comments>
		<pubDate>Fri, 31 Oct 2008 04:52:05 +0000</pubDate>
		<dc:creator>Justin Carmony</dc:creator>
		
		<category><![CDATA[Video Gaming]]></category>

		<category><![CDATA[electronic arts]]></category>

		<category><![CDATA[Frustration]]></category>

		<category><![CDATA[Ranting]]></category>

		<category><![CDATA[Tiger Woods 09]]></category>

		<guid isPermaLink="false">http://www.justincarmony.com/blog/2008/10/30/im-boycotting-electronic-arts-ea-games/</guid>
		<description><![CDATA[As of today, I'm not going to buy another Electronic Arts (aka EA) video game. I'm tired of their "quantity over quality" point of view, and their poor decisions. I've blogged about my problems with EA Games before, but today I read that they are implementing a new system for their forum moderation. Basically, if [...]


Related posts:<ol><li><a href='http://www.justincarmony.com/blog/2008/10/15/ea-lack-of-quality-out-of-touch/' rel='bookmark' title='Permanent Link: EA - Lack of Quality &#038; Out of Touch'>EA - Lack of Quality &#038; Out of Touch</a> <small>An article posted on Slashdot just set me off today....</small></li><li><a href='http://www.justincarmony.com/blog/2008/02/19/starting-book-reviews/' rel='bookmark' title='Permanent Link: Starting Book Reviews'>Starting Book Reviews</a> <small>Alright, to help me continue the learning process in my...</small></li></ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p><img class="alignright" src="http://www.justincarmony.com/blog/wp-content/uploads/2008/10/ealogo.png" alt="ealogo.png" width="160" height="160" />As of today, I'm not going to buy another Electronic Arts (aka EA) video game. I'm tired of their "quantity over quality" point of view, and their poor decisions. <a href="http://www.justincarmony.com/blog/2008/10/15/ea-lack-of-quality-out-of-touch/">I've blogged about my problems with EA Games before</a>, but today I read that <a href="http://games.slashdot.org/article.pl?sid=08/10/31/004213">they are implementing a new system for their forum moderation</a>. Basically, if you get banned on their forums, or any other part of their website, all relating account &amp; games will be banned as well. So for the dozens of EA games that I own that I play online, some volunteer moderator at the EA forums can think my post is out of line, and ban me from all my purchases with EA? Some games aren't played online, but almost all games now have some form of online participation.</p>
<p>If EA wants to punish its consumers so heavily for posting on their forums what might be inappropriate or out of line, that's fine, just ban their forum account. But if you ban their entire online gaming connections, that is going too far. This is my last straw with EA, and until I see significant changes in their attitude towards consumers, they will not receive a single penny from me. My money as a consumer is an award for good companies, and I don't believe in awarding bad companies.</p>
<p><em>Update: it appears EA has <a href="http://www.shacknews.com/onearticle.x/55656" target="_blank">retracted it's statement</a> to clarify that bans are handled individually, not globally. However, until I see better quality from EA, I'm not buying their games, period. </em></p>


<p>Related posts:<ol><li><a href='http://www.justincarmony.com/blog/2008/10/15/ea-lack-of-quality-out-of-touch/' rel='bookmark' title='Permanent Link: EA - Lack of Quality &#038; Out of Touch'>EA - Lack of Quality &#038; Out of Touch</a> <small>An article posted on Slashdot just set me off today....</small></li><li><a href='http://www.justincarmony.com/blog/2008/02/19/starting-book-reviews/' rel='bookmark' title='Permanent Link: Starting Book Reviews'>Starting Book Reviews</a> <small>Alright, to help me continue the learning process in my...</small></li></ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p><img src="http://feeds.feedburner.com/~r/JustinCarmony/~4/437704111" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.justincarmony.com/blog/2008/10/30/im-boycotting-electronic-arts-ea-games/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.justincarmony.com/blog/2008/10/30/im-boycotting-electronic-arts-ea-games/</feedburner:origLink></item>
		<item>
		<title>Father Sued Over Freedom of Speech</title>
		<link>http://feeds.feedburner.com/~r/JustinCarmony/~3/435118538/</link>
		<comments>http://www.justincarmony.com/blog/2008/10/28/father-sued-over-freedom-of-speech/#comments</comments>
		<pubDate>Tue, 28 Oct 2008 20:22:31 +0000</pubDate>
		<dc:creator>Justin Carmony</dc:creator>
		
		<category><![CDATA[General]]></category>

		<category><![CDATA[Linux]]></category>

		<category><![CDATA[Personal]]></category>

		<guid isPermaLink="false">http://www.justincarmony.com/blog/?p=270</guid>
		<description><![CDATA[I thought I would step in and write about the total non-sense that has been going on with the Linspire/Freespire debacle. Today I just found out my father, Kevin Carmony and ex-CEO of Linspire, is being sued by Michael Robertson over "... cybersquatting, trademark infringement, and unfair competition..." for "$100,000 and other damages." You can [...]


Related posts:<ol><li><a href='http://www.justincarmony.com/blog/2008/08/06/funny-iphone-comic/' rel='bookmark' title='Permanent Link: Funny iPhone Comic'>Funny iPhone Comic</a> <small>I haven't written in a few days, and I've been...</small></li><li><a href='http://www.justincarmony.com/blog/about/' rel='bookmark' title='Permanent Link: About'>About</a> <small>Hi, I'm Justin Carmony. I am a professional web designer...</small></li></ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>I thought I would step in and write about the total non-sense that has been going on with the Linspire/Freespire debacle. Today I just found out my father, <a href="http://en.wikipedia.org/wiki/Kevin_Carmony" target="_blank">Kevin Carmony</a> and ex-CEO of <a href="http://www.linspire.com/" target="_blank">Linspire</a>, is being sued by Michael Robertson over "... cybersquatting, trademark infringement, and unfair competition..." for "$100,000 and other damages." You can <a href="http://kevincarmony.blogspot.com/2008/10/michael-robertson-sues-me-to-impede-my.html" target="_self">read the entire post</a> on <a href="http://kevincarmony.blogspot.com/" target="_blank">Kevin's blog</a>. He summarizes the real intent behind this lawsuit well:</p>
<blockquote><p>The lawsuit even states, <span style="font-style: italic;">"Although the FREESPIRE mark was recently assigned as part of an asset purchase, [Robertson] expressly reserved all rights in and to all claims against Carmony arising from the freespire.com domain name and website."</span> Now, why would Robertson want to carve out that one thing to hang on to? He's no longer involved with Freespire, so why does he care? Is he desperate for money? <span style="font-weight: bold; font-style: italic;">With Robertson's companies struggling and laying employees off, does anyone really believe this is the most important thing Robertson should be concerning himself with? </span><span>His motives are pretty transparent--to suppress freedom of expression that is critical of Robertson.</span></p>
<p>This is a nuisance lawsuit, intended to silence my freedom of speech. <span style="font-weight: bold; font-style: italic;"> I will defend my right to free speech by continuing to point freespire.com to my blog and keep reporting on Robertson's disturbing actions.</span></p></blockquote>
<p>Enough is enough Michael. You once stated that the <a href="http://www.michaelrobertson.com/archive.php?minute_id=266" target="_blank">"The Record Labels Want My Minivan"</a> playing the victim of a personal lawsuit. Seems rather hypocritical now that you're suing my father. What about my stocks in Linspire? I was hoping to use them to help pay for school or a down payment on my house. But of course my house, car, or personal money doesn't matter to you Michael, since you drove Linspire into the ground and sold it's assets off in a single year. I'm glad to see you valued the shareholders to much. Seems its only a crime when its "your minivan" at risk.</p>
<p>If you want to read up more on Michael Robertson &amp; the Linspire/Freespire issue, you can checkout my <a href="http://kevincarmony.blogspot.com/" target="_blank">father's blog</a>.</p>


<p>Related posts:<ol><li><a href='http://www.justincarmony.com/blog/2008/08/06/funny-iphone-comic/' rel='bookmark' title='Permanent Link: Funny iPhone Comic'>Funny iPhone Comic</a> <small>I haven't written in a few days, and I've been...</small></li><li><a href='http://www.justincarmony.com/blog/about/' rel='bookmark' title='Permanent Link: About'>About</a> <small>Hi, I'm Justin Carmony. I am a professional web designer...</small></li></ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p><img src="http://feeds.feedburner.com/~r/JustinCarmony/~4/435118538" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.justincarmony.com/blog/2008/10/28/father-sued-over-freedom-of-speech/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.justincarmony.com/blog/2008/10/28/father-sued-over-freedom-of-speech/</feedburner:origLink></item>
		<item>
		<title>PHP Design - Biggest Database Oversights</title>
		<link>http://feeds.feedburner.com/~r/JustinCarmony/~3/431535814/</link>
		<comments>http://www.justincarmony.com/blog/2008/10/25/php-design-biggest-database-oversights/#comments</comments>
		<pubDate>Sat, 25 Oct 2008 07:56:44 +0000</pubDate>
		<dc:creator>Justin Carmony</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Database]]></category>

		<category><![CDATA[Designs &amp; Patterns]]></category>

		<category><![CDATA[MySQL]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Tips and Tricks]]></category>

		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.justincarmony.com/blog/?p=261</guid>
		<description><![CDATA[Over the last three years I've had the opportunity to work on several PHP projects, some of them having grown rapidly and required to scale quickly. Three in particular have been a fantastic learning experience for me. Now I don't consider myself a total expert, but I thought I would share my experiences. Many PHP [...]


Related posts:<ol><li><a href='http://www.justincarmony.com/blog/2008/07/01/mysql-php-sql_calc_found_rows-an-easy-way-to-get-the-total-number-of-rows-regardless-of-limit/' rel='bookmark' title='Permanent Link: MySQL &#038; PHP  - SQL_CALC_FOUND_ROWS - An easy way to get the total number of rows regardless of LIMIT'>MySQL &#038; PHP  - SQL_CALC_FOUND_ROWS - An easy way to get the total number of rows regardless of LIMIT</a> <small>Here is a little trick I found awhile back. I...</small></li><li><a href='http://www.justincarmony.com/blog/2008/11/03/mysql-does-table-exist-wo-throwing-errors/' rel='bookmark' title='Permanent Link: MySQL - Does Table Exist w/o Throwing Errors'>MySQL - Does Table Exist w/o Throwing Errors</a> <small>There are times where you would like to know if...</small></li><li><a href='http://www.justincarmony.com/blog/2008/03/07/five-common-php-design-patterns-that-arent-used-enough/' rel='bookmark' title='Permanent Link: Five common PHP design patterns that aren&#8217;t used enough'>Five common PHP design patterns that aren&#8217;t used enough</a> <small>"This article explains five widely used design patterns in PHP...</small></li></ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>Over the last three years I've had the opportunity to work on several PHP projects, some of them having grown rapidly and required to scale quickly. Three in particular have been a fantastic learning experience for me. Now I don't consider myself a total expert, but I thought I would share my experiences. Many PHP developers are self-taught, like I was, and are learning PHP to be able to create dynamic websites. Many haven't had studied or learned best practices for Object-Oriented Programming (OOP). They are web designers first, and programming in PHP is just a way to do things simple HTML can't do.</p>
<p>Because most PHP applications rely heavily on the ability to read and store information in a database, many times designing how you interact with that database is critical. The reason being is if you design something, but don't foresee a requirement or necessity, it can be extremely time consuming to go back and change. These "oversights" can be costly in time, money, and resources.</p>
<p>I've thought of some of the biggest oversights I've had when working with PHP and MySQL and put them in a list. This is my personal list, and I'm sure some people can think of some other oversights that belong on the list as well. This list is just for PHP &amp; MySQL, not PHP and any database. I know many people like using software like <a href="http://www.doctrine-project.org/">Doctrine</a> to allow switching between different database types. That is beyond the scope of this article.</p>
<p>Many of the examples in this article are very simple and are designed for showing techniques of better programming, not for actual use. Straight copy and paste most likely will not create sound, robust solutions. Search the web for a solid MySQL PHP Class to handle these issues for you, or you can make your own.</p>
<p>Now that I've thrown out my disclaimers and thoughts, lets venture on to by list:</p>
<p><strong></p>
<ul>
<li>Oversight #1 - No Data Access Layer</li>
<li>Oversight #2 - Design for Only One Database Connection</li>
<li>Oversight #3 - No Developer Logging</li>
<li>Oversight #4 - Queries Written In Procedural Processes</li>
<li>Oversight #5 - No Separation of Reads &#038; Writes</li>
</ul>
<p></strong></p>
<h2>Oversight #1 - No Data Access Layer</h2>
<p>I first heard the term Data Access Layer was when I was reading about ASP .NET. Basically a Data Access Layer (DAL) is a layer of code that you access data through. Sounds simple right? Many new php developers don't have any type of DAL. Here is an example of not having a Data Access Layer:</p>
<div class="igBar"><span id="lphp-52"><a href="#" onclick="javascript:showPlainTxt('php-52'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-52">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">&lt;?php</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Create MySQL Connection</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$connection</span> = <a href="http://www.php.net/mysql_connect"><span style="color:#000066;">mysql_connect</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'localhost'</span>, <span style="color:#FF0000;">'mysql_user'</span>, <span style="color:#FF0000;">'mysql_password'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Query User List</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$result</span> = <a href="http://www.php.net/mysql_query"><span style="color:#000066;">mysql_query</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"SELECT * FROM users"</span>, <span style="color:#0000FF;">$connection</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Get User List in an Array</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$user_list</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$user</span> = <a href="http://www.php.net/mysql_fetch_assoc"><span style="color:#000066;">mysql_fetch_assoc</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$result</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;<span style="color:#0000FF;">$user_list</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#0000FF;">$user</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Take the populated user list and display a table...</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// ..</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">?&gt;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Now, that wasn't so bad, was it? Lots of developers just use the mysql commands. I have several projects I worked on my early days with PHP that fall victim to this oversight. One in particular now has grown out of hand so bad that we've decided to start from scratch for a whole new version. Why? Lets say you have 3000+ php files, and your boss says "Hrm, we're seeing some problems with performance. Can you display at the bottom of each page the # of queries you use on that page?" If you coded your entire project like the example above, you would be totally screwed. You would have to find each and every mysql_query() and add some counter at the end. It would be a managing Nightmare. So how cold you solve this problem?</p>
<div class="igBar"><span id="lphp-53"><a href="#" onclick="javascript:showPlainTxt('php-53'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-53">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">&lt;?php</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Declare MySqlDAL class</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">class</span> MySqlDAL </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;public <span style="color:#0000FF;">$connection</span> = <span style="color:#000000; font-weight:bold;">null</span>; <span style="color:#FF9933; font-style:italic;">// hold the connection link resource</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;public <span style="color:#0000FF;">$query_count</span> = <span style="color:#CC66CC;color:#800000;">0</span>; <span style="color:#FF9933; font-style:italic;">// total number of queries executed</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;<span style="color:#FF9933; font-style:italic;">// Connect when creating an instance of this class</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;public <span style="color:#000000; font-weight:bold;">function</span> __construct<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$host</span>, <span style="color:#0000FF;">$user</span>, <span style="color:#0000FF;">$pass</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;<span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">connection</span> = <a href="http://www.php.net/mysql_connect"><span style="color:#000066;">mysql_connect</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$host</span>, <span style="color:#0000FF;">$user</span>, <span style="color:#0000FF;">$pass</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">true</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;<span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;<span style="color:#FF9933; font-style:italic;">// Run a query against the database. The key here is to make sure </span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;<span style="color:#FF9933; font-style:italic;">// every query command goes through this function</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;public <span style="color:#000000; font-weight:bold;">function</span> Query<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$sql</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;<span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// Execute Query &amp; Get Result Resource</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$result</span> = <a href="http://www.php.net/mysql_query"><span style="color:#000066;">mysql_query</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$sql</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// Increment Query Counter</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">query_count</span>++;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// Return the result</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#0000FF;">$result</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;<span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;<span style="color:#FF9933; font-style:italic;">// Function to take an SQL query, execute it, and return all </span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;<span style="color:#FF9933; font-style:italic;">// the rows in an assoc. array.</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;public <span style="color:#000000; font-weight:bold;">function</span> FetchArray<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$sql</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;<span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// Execute the Query and get the Result</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$result</span> = <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">query</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$sql</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// Create Empty Array to Store all the rows</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$array</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// Loop through each row</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$row</span> = <a href="http://www.php.net/mysql_fetch_assoc"><span style="color:#000066;">mysql_fetch_assoc</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$result</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#FF9933; font-style:italic;">// Add the row to the array</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#0000FF;">$array</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#0000FF;">$row</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// Return the array containing all the rows</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#0000FF;">$array</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;<span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$dal</span> = <span style="color:#000000; font-weight:bold;">new</span> MySqlDAL<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'localhost'</span>,<span style="color:#FF0000;">'mysql_user'</span>,<span style="color:#FF0000;">'mysql_pass'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$user_list</span> = <span style="color:#0000FF;">$dal</span>-&gt;<span style="color:#006600;">FetchArray</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"SELECT * FROM users"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Display Table of Users...</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// ..</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">?&gt;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>First of all, you've satisfied the boss's wish of keeping a counter for the total # of queries. The trick is to have all queries go through the function "Query($sql)." This will ensure the counter stays current. You can also add code to catch errors, display error messages, log queries, etc. Having a class to wrap around your database connection, queries, etc. can make your life easier, and more simple.</p>
<p><span id="more-261"></span><br />
<h2>Oversight #2 - Design for Only One Database Connection</h2>
<p>I have a project that I've taken over right now that is suffering from being programmed into handling only one database connection. "But why would you ever need to connect to more than one database?" Well, I like to have a database for important information: invoices, users, payments, etc. Stuff that is updated every day &#038; information that the end user manipulates. Then I like to have another database for logging statistical information: user log-ins, page hits, visitor counts. The problem I've ran into lately is I want to have hourly backups for my important information, but only daily backups for simple statistical information. It would be much easier if they were separated in different databases, but many times they are in the same. I don't want to take the extra time and space to backup the statistical information.</p>
<p>Many people who run into this problem use a <a href="http://en.wikipedia.org/wiki/Singleton_pattern">Singleton class</a> to handle their Data Access Layer. A singleton class is a class that can only have one instance at a time in execution. This can be suer easy and quick to use in your code, but may lock you into a situation where your code only handles a single database connection:</p>
<div class="igBar"><span id="lphp-54"><a href="#" onclick="javascript:showPlainTxt('php-54'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-54">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">&lt;?php</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// I've declared a singleton class called MyDB.</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$users</span> = MyDb::<span style="color:#006600;">FetchArray</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"SELECT * FROM users"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// ..</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">?&gt;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Now your boss comes in and asks you to connect to another database to get a list of customers. You think "uh oh, I can't really do that with my design, now can I?" The solution I like to use is having Database Connection classes, and then a Database Connection Manager. Imagine I have the MySqlDAL class from the previous example in oversight #1. I would create singleton class called MySqlManager. It's job would be to handle the creation &#038; management of all it's classes. Here is an example: </p>
<div class="igBar"><span id="lphp-55"><a href="#" onclick="javascript:showPlainTxt('php-55'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-55">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">&lt;?php</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// ... declare my MySqlDAL class before</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Handles the creation and management of MySQL Connections</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">class</span> MySqlManager</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// Holds the singleton instance</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; private <a href="http://www.php.net/static"><span style="color:#000066;">static</span></a> <span style="color:#0000FF;">$instance</span> = <span style="color:#000000; font-weight:bold;">null</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// Contians each MySqlDAL object created;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; private <span style="color:#0000FF;">$connections</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; private <span style="color:#000000; font-weight:bold;">function</span> __construct<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// Its private to prevent creation outside of the GetInstance function</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">connections</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; public <a href="http://www.php.net/static"><span style="color:#000066;">static</span></a> <span style="color:#000000; font-weight:bold;">function</span> GetInstance<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// If the instance is null, make one</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span>!self::<span style="color:#0000FF;">$instance</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self::<span style="color:#0000FF;">$instance</span> = <span style="color:#000000; font-weight:bold;">new</span> MySqlManager<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> self::<span style="color:#0000FF;">$instance</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// Connect to a new database;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; public <a href="http://www.php.net/static"><span style="color:#000066;">static</span></a> <span style="color:#000000; font-weight:bold;">function</span> CreateConnection<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$host</span>, <span style="color:#0000FF;">$user</span>, <span style="color:#0000FF;">$pass</span>, <span style="color:#0000FF;">$name</span> = <span style="color:#FF0000;">'default'</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$manager</span> = self::<span style="color:#006600;">GetInstance</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$manager</span>-&gt;<span style="color:#006600;">connections</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF;">$name</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#000000; font-weight:bold;">new</span> MySqlDAL<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$host</span>, <span style="color:#0000FF;">$user</span>, <span style="color:#0000FF;">$pass</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#0000FF;">$manager</span>-&gt;<span style="color:#006600;">connections</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF;">$name</span><span style="color:#006600; font-weight:bold;">&#93;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; public <a href="http://www.php.net/static"><span style="color:#000066;">static</span></a> <span style="color:#000000; font-weight:bold;">function</span> GetConnection<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$name</span> = <span style="color:#FF0000;">'default'</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$manager</span> = self::<span style="color:#006600;">GetInstance</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/isset"><span style="color:#000066;">isset</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$manager</span>-&gt;<span style="color:#006600;">connections</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF;">$name</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#0000FF;">$manager</span>-&gt;<span style="color:#006600;">connections</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF;">$name</span><span style="color:#006600; font-weight:bold;">&#93;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">else</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// handle connection not found error...</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Now you can create a connection, and if you don't give it a name, it will be defaulted for you</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$dal</span> = MySqlManager::<span style="color:#006600;">CreateConnection</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'localhost'</span>,<span style="color:#FF0000;">'mysql_user'</span>,<span style="color:#FF0000;">'mysql_pass'</span>, <span style="color:#FF0000;">'customer'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$customer_dal</span> = MySqlManager::<span style="color:#006600;">CreateConnection</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'mysql.another-location.com'</span>,<span style="color:#FF0000;">'another_user'</span>,<span style="color:#FF0000;">'another_pass'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// ..</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// later in your code, maybe even in a class or function you could get the connection by name:</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// get default connection</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$dal</span> = MySqlManager::<span style="color:#006600;">GetConnection</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Get customer database connection</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$customer_dal</span> = MySqlManager::<span style="color:#006600;">GetConnection</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'customer'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$users</span> = <span style="color:#0000FF;">$dal</span>-&gt;<span style="color:#006600;">FetchArray</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'SELECT * FROM users'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$customers</span> = <span style="color:#0000FF;">$customer_dal</span>-&gt;<span style="color:#006600;">FetchArray</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"SELECT * FROM customers"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// ..</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">?&gt;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Now you have a MySQL Connection Manager class that can handle the creation, management, and disconnection of your SQL connections. Your boss will be happy to hear you can connect to another database with easy!</p>
<h2>Oversight #3 - No Developer Logging</h2>
<p>There are many times where I wish I could what was going on behind the scenes. It is very easy to add Query Logging to your Data Access Layer if you choose. I won't give an example how to do so, but there are some several very important parts to it. First, you need to be able to enable &#038; disable query logging. Second, you should log whether it was successful or not. Also,  if you play around with PHP's back tracing abilities you can trace where each query was made. I'll make a blog post about this another day. </p>
<h2>Oversight #4 - Queries Written In Procedural Processes</h2>
<p>I absolutely *loathe* when I get into a situation where queries are being written all over the place. If there is one constant in any software development, it is that things will always change. When queries are being placed in procedural code, it makes management of database changes a total hassle. "But what about find and replace?" That can help you find places where you need to change, but you still can miss other places in your code, which can lead to bugs, headaches, and self-combustion. Here is an example: </p>
<div class="igBar"><span id="lphp-56"><a href="#" onclick="javascript:showPlainTxt('php-56'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-56">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">&lt;?php</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// .. code displaying a user profile:</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Get user links</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$links</span> = <span style="color:#0000FF;">$dal</span>-&gt;<span style="color:#006600;">FetchArray</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"SELECT * FROM user_links WHERE user_id = 1 ORDER BY number"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Display them on links page...</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// .. another page</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Get user links</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$links</span> = <span style="color:#0000FF;">$dal</span>-&gt;<span style="color:#006600;">FetchArray</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"SELECT * FROM user_links ORDER BY number"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Display them on profile page...</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// .. another page</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Get user links</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$links</span> = <span style="color:#0000FF;">$dal</span>-&gt;<span style="color:#006600;">FetchArray</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"SELECT * FROM user_links ORDER BY number"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Display them on about me page...</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">?&gt;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Not seeing the big issue here? Lets say you copy and paste that line of code '$links = $dal->FetchArray("SELECT * FROM user_links ORDER BY id");' on twenty different pages. Then your boss comes and says "Hey, you know how the user_links table has a title? Well every where we display the title, I'd like to have it show which # of link it is, so example: Link #1 - My Blog, Link #2 - My Facebook., Oh, and can we order them by date_added? Thanks!"</p>
<p>Have fun trying to find every place that query is executed. Don't be surprised when only 80% of the queries get changed, and the other 20% become bug tickets with your boss wondering why you didn't find them before. This is an example of writing queries all over the place when you need them, instead of organizing them and abstracting them by Classes and Functions. Here is an example of abstracting the query:</p>
<div class="igBar"><span id="lphp-57"><a href="#" onclick="javascript:showPlainTxt('php-57'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-57">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">&lt;?php</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">class</span> User</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;public <span style="color:#0000FF;">$id</span>; <span style="color:#FF9933; font-style:italic;">// contains user_id </span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color:#FF9933; font-style:italic;">// ..</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> GetVideos<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$dal</span> = MySqlManager::<span style="color:#006600;">GetInstance</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$videos</span> = <span style="color:#0000FF;">$dal</span>-&gt;<span style="color:#006600;">FetchArray</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"SELECT * FROM user_videos WHERE user_id = '$this-&gt;id' ORDER BY date_added"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$new_video_list</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">foreach</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$videos</span> <span style="color:#616100;">as</span> <span style="color:#0000FF;">$video</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$video</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'caption'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#FF0000;">"Link #"</span>.<span style="color:#0000FF;">$video</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'number'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#FF0000;">' - '</span>.<span style="color:#0000FF;">$video</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'caption'</span><span style="color:#006600; font-weight:bold;">&#93;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$new_video_list</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#0000FF;">$video</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#0000FF;">$new_video_list</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// ..</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$user</span> = <span style="color:#000000; font-weight:bold;">new</span> User<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$user_videos</span> = <span style="color:#0000FF;">$user</span>-&gt;<span style="color:#006600;">GetVideos</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// ..</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">?&gt;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Now your boss will give you a raise when you can make changes this quickly. While this example may seem simple, there are many times where I've had projects that queries have required major changes and re-writes. Bugs quickly service if queries that are the same, or similar, are copied and re-copied all over the place. This leads to a giant mess very quickly. So remove queries from the procedural flow of things by abstracting them in classes and functions.</p>
<h2>Oversight #5 - No Separation of Reads &amp; Writes</h2>
<p>This oversight has stung me for the first time just a few weeks ago. A project of mine had reached critical mass with its size, and out grew its beefy MySQL dedicated server. I started investigating doing a Master-Slave replication system, but every article said I needed to route SELECT statements to the slave servers, and just about every else to the Master (like Insert, Update, Delete, etc). This project didn't have any Data Access Layer at all, and 2+ years of development on it. There is no way I could go back and re-write all the queries on budget and timeline, especially without causing bugs. especially if Insert/Update/Delete commands on accidently got routed to a slave server.</p>
<p>My suggestion is funneling all queries into two functions, Select() and Execute(). Then, when you create a MySqlDAL instance, it would actually have a $this->master_connection; and $this->slave_connection; It wouldn't connection on creation, but on the first instance a query was executed. This is done to release stress on the Master server, the whole purpose of Master-Slave replication setups. Here is a mock class that would handle this:</p>
<div class="igBar"><span id="lphp-58"><a href="#" onclick="javascript:showPlainTxt('php-58'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-58">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">&lt;?php</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">class</span> MySqlDAL</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; public <span style="color:#0000FF;">$master_connection</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; public <span style="color:#0000FF;">$master_host</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; public <span style="color:#0000FF;">$slave_connection</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; public <span style="color:#0000FF;">$slave_host</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; public <span style="color:#0000FF;">$user</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; public <span style="color:#0000FF;">$pass</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// ..</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> Select<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$sql</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A