<?xml version="1.0" encoding="utf-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	>
<channel>
	<title>Comments on: How do you code your DB calls?</title>
	<atom:link href="http://robert.accettura.com/blog/2004/06/01/how-do-you-code-your-db-calls/feed/" rel="self" type="application/rss+xml" />
	<link>http://robert.accettura.com/blog/2004/06/01/how-do-you-code-your-db-calls/</link>
	<description>Robert Accettura's Personal Blog on Web Development and Tech</description>
	<pubDate>Fri,  9 Jan 2009 22:30:05 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Jan!</title>
		<link>http://robert.accettura.com/blog/2004/06/01/how-do-you-code-your-db-calls/comment-page-1/#comment-957</link>
		<dc:creator>Jan!</dc:creator>
		<pubDate>Wed, 31 Dec 1969 18:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://robert.accettura.com/archives/2004/06/01/how-do-you-code-your-db-calls/#comment-957</guid>
		<description>I semi-hardcode it:

$tbl_people = 'my_people';
$fld_firstname = 'first_name';

and for the queries:

mysql_query("SELECT $fld_firstname FROM $tbl_people WHERE ")

That way I can type and edit the query where it's used in the code, and still have some more central control over the table and field names.

PS: Could you add some padding on your comments pages' body? It's a bit hard to read when the page is in a fullscreen tab or window.</description>
		<content:encoded><![CDATA[<p>I semi-hardcode it:</p>
<p>$tbl_people = &#8216;my_people&#8217;;<br />
$fld_firstname = &#8216;first_name&#8217;;</p>
<p>and for the queries:</p>
<p>mysql_query(&#8221;SELECT $fld_firstname FROM $tbl_people WHERE &#8220;)</p>
<p>That way I can type and edit the query where it&#8217;s used in the code, and still have some more central control over the table and field names.</p>
<p>PS: Could you add some padding on your comments pages&#8217; body? It&#8217;s a bit hard to read when the page is in a fullscreen tab or window.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Antony</title>
		<link>http://robert.accettura.com/blog/2004/06/01/how-do-you-code-your-db-calls/comment-page-1/#comment-958</link>
		<dc:creator>Antony</dc:creator>
		<pubDate>Wed, 31 Dec 1969 18:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://robert.accettura.com/archives/2004/06/01/how-do-you-code-your-db-calls/#comment-958</guid>
		<description>If you use a database that supports views (eg. Postgresql), you can save yourself the pain and trauma caused by underlying database structure changes by creating a view that replicates the old structure using your new tables, thus saving the headache of having to update your code.

As an example, say you had a table called 'users' that has the fields you described. Now imagine for whatever obscure reason you broke this into two separate tables -- users_brief and users_details. You import your old data into these two tables, then delete your old users table. Now you define the 'users' VIEW, which for all intents and purposes you can use as another table in your query. A view basically allows you to define a "virtual table" that is defined as an SQL query -- so you could have a query that does a SELECT * FROM users_brief LEFT JOIN user_details USING (user_id), or something to that effect, and present that as the 'users' table. Rename fields, whatever is necessary, and your underlying application will never know the difference.

You can then update your application to the new code (if you even want to) at your leisure - there's no critical rush to get all the queries changed over while users are clammouring at you about not being able to access the system.....

Because the view is defined within the actual DBMS, the query planner should have the smarts to optimise it and ensure the performance difference between the view and the regular table is minimal.

If you want any more information/help drop me an email :-)

Nothing stops you from updating your code to the new</description>
		<content:encoded><![CDATA[<p>If you use a database that supports views (eg. Postgresql), you can save yourself the pain and trauma caused by underlying database structure changes by creating a view that replicates the old structure using your new tables, thus saving the headache of having to update your code.</p>
<p>As an example, say you had a table called &#8216;users&#8217; that has the fields you described. Now imagine for whatever obscure reason you broke this into two separate tables &#8212; users_brief and users_details. You import your old data into these two tables, then delete your old users table. Now you define the &#8216;users&#8217; VIEW, which for all intents and purposes you can use as another table in your query. A view basically allows you to define a &#8220;virtual table&#8221; that is defined as an <acronym title="Structured Query Language (a database standard)">SQL</acronym> query &#8212; so you could have a query that does a SELECT * FROM users_brief LEFT JOIN user_details USING (user_id), or something to that effect, and present that as the &#8216;users&#8217; table. Rename fields, whatever is necessary, and your underlying application will never know the difference.</p>
<p>You can then update your application to the new code (if you even want to) at your leisure - there&#8217;s no critical rush to get all the queries changed over while users are clammouring at you about not being able to access the system&#8230;..</p>
<p>Because the view is defined within the actual DBMS, the query planner should have the smarts to optimise it and ensure the performance difference between the view and the regular table is minimal.</p>
<p>If you want any more information/help drop me an email <img src='http://robert.accettura.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /><br />
Nothing stops you from updating your code to the new</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: teddybertrand</title>
		<link>http://robert.accettura.com/blog/2004/06/01/how-do-you-code-your-db-calls/comment-page-1/#comment-959</link>
		<dc:creator>teddybertrand</dc:creator>
		<pubDate>Wed, 31 Dec 1969 18:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://robert.accettura.com/archives/2004/06/01/how-do-you-code-your-db-calls/#comment-959</guid>
		<description>i just hardcode it all
this may be harder to change when you have a modification in the db structure but in fact i like to see clearly which query i'm sending to the db. i wouldn't use function because i would always ask myself what syntax i must use to pass my arguments (quotes or not quotes, etc.).
semi-hardcoding like Jan! seems interesting but i would feel a bit lost if a variable called $name contained "surname" just because i renamed a col from name to surname.
and of course views are a very good solution but i'm pretty sure MySQL don't have them</description>
		<content:encoded><![CDATA[<p>i just hardcode it all<br />
this may be harder to change when you have a modification in the db structure but in fact i like to see clearly which query i&#8217;m sending to the db. i wouldn&#8217;t use function because i would always ask myself what syntax i must use to pass my arguments (quotes or not quotes, etc.).<br />
semi-hardcoding like Jan! seems interesting but i would feel a bit lost if a variable called $name contained &#8220;surname&#8221; just because i renamed a col from name to surname.<br />
and of course views are a very good solution but i&#8217;m pretty sure MySQL don&#8217;t have them</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Antony</title>
		<link>http://robert.accettura.com/blog/2004/06/01/how-do-you-code-your-db-calls/comment-page-1/#comment-960</link>
		<dc:creator>Antony</dc:creator>
		<pubDate>Wed, 31 Dec 1969 18:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://robert.accettura.com/archives/2004/06/01/how-do-you-code-your-db-calls/#comment-960</guid>
		<description>From their site: "Support for stored procedures was added in version 5.0, and support for triggers will be added in version 5.1." Given that 4.0 is the latest stable it could be a while...

I cut my teeth on MySQL many years ago, but after running into some of its limitations around the same time I was studying database theory and design made me search around for other options. I decided to give Postgresql a try and have never looked back. 

Answering the original question, I do a couple of things:

I have a home grown PHP database wrapper class that handles all my required functionality.. if I ever switch DBMSes, I have a single place to go and change the database specific functions (things in queries is a different story -- for that you probably want to look at ADODB or something that attempts to isolate differences in SQL implementations). If I want to debug an application, I can set a flag to output the queries and/or results to follow whats going on. Really handy.

In my queries, I make sure I'm only SELECTing the fields I need. Performance aspects aside, think of security. If you're pulled every bit of data from the database and the user finds a way to display it, you're screwed. If you only pull out information you were planning on using, the impact is dramatically reduced. If I ever have some reason to change the underlying database structure, I know I can always emulate the old table using a view.

It all depends on just how many queries you're going to have. If you're going to be reusing a lot of the same queries, you might even be worth looking at a 3 tier design:

    DBase  Business Logic/Rules  UI

That way, you can isolate your queries in the database tier, your checks and manipulation in the business logic tier, and your main PHP does the UI. All you need to do is create an instance of your business logic class and use the data provided by it in whatever way you desire in the UI.

On the downside, you add overheads - it takes time to design in a tiered approach rather than an ad-hoc amalgamated design, and in any form of abstraction you add processing overheads. In the end its a tradeoff between maintainability and separation between your functional areas and getting things done in a hurry :-)

On the issue of logic/UI separation, I've found Smarty (&lt;a href="http://smarty.php.net/)"&gt;http://smarty.php.net/)&lt;/a&gt; to be absolutely superb. Having all my HTML and other display tucked away in a separate file and leaving the PHP code to do all the logic and input handling really makes things cleaner to work with.
</description>
		<content:encoded><![CDATA[<p>From their site: &#8220;Support for stored procedures was added in version 5.0, and support for triggers will be added in version 5.1.&#8221; Given that 4.0 is the latest stable it could be a while&#8230;</p>
<p>I cut my teeth on MySQL many years ago, but after running into some of its limitations around the same time I was studying database theory and design made me search around for other options. I decided to give Postgresql a try and have never looked back. </p>
<p>Answering the original question, I do a couple of things:</p>
<p>I have a home grown <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> database wrapper class that handles all my required functionality.. if I ever switch DBMSes, I have a single place to go and change the database specific functions (things in queries is a different story &#8212; for that you probably want to look at ADODB or something that attempts to isolate differences in <acronym title="Structured Query Language (a database standard)">SQL</acronym> implementations). If I want to debug an application, I can set a flag to output the queries and/or results to follow whats going on. Really handy.</p>
<p>In my queries, I make sure I&#8217;m only SELECTing the fields I need. Performance aspects aside, think of security. If you&#8217;re pulled every bit of data from the database and the user finds a way to display it, you&#8217;re screwed. If you only pull out information you were planning on using, the impact is dramatically reduced. If I ever have some reason to change the underlying database structure, I know I can always emulate the old table using a view.</p>
<p>It all depends on just how many queries you&#8217;re going to have. If you&#8217;re going to be reusing a lot of the same queries, you might even be worth looking at a 3 tier design:</p>
<p>    DBase  Business Logic/Rules  <acronym title="User Interface">UI</acronym></p>
<p>That way, you can isolate your queries in the database tier, your checks and manipulation in the business logic tier, and your main <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> does the <acronym title="User Interface">UI</acronym>. All you need to do is create an instance of your business logic class and use the data provided by it in whatever way you desire in the <acronym title="User Interface">UI</acronym>.</p>
<p>On the downside, you add overheads - it takes time to design in a tiered approach rather than an ad-hoc amalgamated design, and in any form of abstraction you add processing overheads. In the end its a tradeoff between maintainability and separation between your functional areas and getting things done in a hurry <img src='http://robert.accettura.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /><br />
On the issue of logic/UI separation, I&#8217;ve found Smarty (<a href="http://smarty.php.net/)"></a><a href="http://smarty.php.net/" rel="nofollow">http://smarty.php.net/</a>) to be absolutely superb. Having all my <acronym title="HyperText Markup Language">HTML</acronym> and other display tucked away in a separate file and leaving the <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> code to do all the logic and input handling really makes things cleaner to work with.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
