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

<channel>
	<title>giancarlo.dimassa.net &#187; Database</title>
	<atom:link href="http://giancarlo.dimassa.net/category/php/database/feed/" rel="self" type="application/rss+xml" />
	<link>http://giancarlo.dimassa.net</link>
	<description>A web programmer&#039;s blog</description>
	<lastBuildDate>Mon, 26 Sep 2011 01:30:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Database abstraction in PHP with MDB2</title>
		<link>http://giancarlo.dimassa.net/2009/01/18/database-abstraction-in-php-with-mdb2/</link>
		<comments>http://giancarlo.dimassa.net/2009/01/18/database-abstraction-in-php-with-mdb2/#comments</comments>
		<pubDate>Sun, 18 Jan 2009 17:20:05 +0000</pubDate>
		<dc:creator>giancarlo</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Pear]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://giancarlo.dimassa.net/2009/01/18/database-abstraction-in-php-with-mdb2/</guid>
		<description><![CDATA[Coming from a Windows environment, the first thing I noticed about PHP was the lack of a standardized way to access databases across vendors. That is, you want to connect to a MySQL server, you use the command mysql_connect(); , for PostgreSQL it is pg_connect(); , for Microsoft SQL server is mssql_connect(); . On Windows, [...]]]></description>
			<content:encoded><![CDATA[<p>Coming from a Windows environment, the first thing I noticed about PHP was the <strong>lack of a standardized way</strong> to access databases <strong>across vendors</strong>. That is, you want to connect to a MySQL server, you use the command mysql_connect(); , for PostgreSQL it is pg_connect(); , for Microsoft SQL server is mssql_connect(); .</p>
<p><span id="more-169"></span></p>
<p>On <strong>Windows</strong>, instead, there is a layer called Open Database Connectivity (<strong>ODBC</strong>) that translates the calls from the software to the database, providing a <strong>common interface</strong> and <strong>emulating missing features</strong>. Each vendor provides a <strong>specific driver</strong>, that plugs into the architecture. Via ODBC even a <strong>text file</strong> or an <strong>Excel spreadsheet</strong> can be <strong>connected</strong> and <strong>queried</strong> like it was a <strong>database</strong>.</p>
<p>It has been natural to me to start searching the net for a PHP equivalent of that layer. Shortly after starting I found the <strong>de facto standard</strong> for database abstraction in PHP, called <strong>MDB2</strong>. Even if it has <strong>not the flexibility</strong> of a real ODBC layer, it&#8217;s <strong>lack of complexity</strong> made it very popular among fellow web programmers at first, just to find out it&#8217;s <strong>ability to grow</strong> via <strong>extensions</strong> and performing in a <strong>relatively easy way</strong> a set of very <strong>powerful</strong> and <strong>demanding operations</strong>.</p>
<p>MDB2 is a <strong>Pear</strong> library, so you need to follow a procedure to install it. Refer to the <a target="_blank" href="http://pear.php.net" >Pear website</a> for details.</p>
<p>Just after <strong>including</strong> the MDB2 scripts into your PHP file</p>
<blockquote><p>&lt;?</p>
<p>require_once (&#8220;MDB2.php&#8221;);</p>
<p>?&gt;</p></blockquote>
<p>you need toÂ  create an <strong>instance</strong> of the database class, like this</p>
<blockquote><p>&lt;?</p>
<p>$dsn = &#8220;your database type://your user name:your password@your server/your database name&#8221;;</p>
<p>$db = MDB2::factory($dsn);</p>
<p>?&gt;</p></blockquote>
<p>so you have a <strong>connection string</strong> that explains how to connect to the database, and an <strong>object</strong> with multiple methods to perform database operations. You don&#8217;t need to know other details about the database apart from the ones you put in the Data Source Name (<strong>DSN</strong>), everything else is handled by the MDB2 object.</p>
<p>After that, you perform <strong>vendor agnostic</strong> <strong>queries</strong> like this</p>
<blockquote><p>$result = $db-&gt;queryAll ($sql);</p></blockquote>
<p>But, as said, the real power behind MDB2 is it&#8217;s <strong>extensions system</strong>, for example</p>
<blockquote><p>$db-&gt;loadModule(&#8216;Extended&#8217;);</p>
<p>$tablename = &#8220;customers&#8221;;</p>
<p>$data = array(&#8220;name&#8221;=&gt;&#8221;Bob&#8221;,&#8221;surname&#8221;=&gt;&#8221;Michael&#8221;);</p>
<p>$result= $db-&gt;extended-&gt;autoExecute($tablename,$data, MDB2_AUTOQUERY_INSERT);</p></blockquote>
<p>I&#8217;ve effectively <strong>inserted</strong> a new customer into my table <strong>without writing a single word of SQL</strong>, and without <strong>worrying</strong> of the actual<strong> table structure</strong>.</p>
<p>The same applies to <strong>interrogation queries</strong>, like this</p>
<blockquote><p>$tablename = &#8220;customers&#8221;;</p>
<p>$where = &#8220;`NAME` = &#8216;BOB&#8217; AND `SURNAME` = &#8216;MICHAEL&#8217;&#8221;;</p>
<p>$result= $db-&gt;extended-&gt;autoExecute($tablename,null,<br />
MDB2_AUTOQUERY_SELECT,<br />
$where,null,true,true);</p></blockquote>
<p>the only SQL here is the <strong>where clause</strong>, everything else is managed by the library.</p>
<p>You can even store an <strong>XML backup</strong> of your entire database with a few commands</p>
<blockquote><p>$db_schema_options = array(<br />
&#8216;use_transactions&#8217; =&gt; true,<br />
&#8216;log_line_break&#8217; =&gt; &#8216;&lt;br&gt;&#8217;,<br />
&#8216;idxname_format&#8217; =&gt; &#8216;%s&#8217;,<br />
&#8216;debug&#8217; =&gt; true,<br />
&#8216;quote_identifier&#8217; =&gt; true,<br />
&#8216;force_defaults&#8217; =&gt; true,<br />
&#8216;portability&#8217; =&gt; true<br />
);</p>
<p>$schema = MDB2_Schema::factory($dsn, $db_schema_options);</p>
<p>$definition = $schema-&gt;getDefinitionFromDatabase();</p>
<p>$schema_dump = $schema-&gt;dumpDatabase($definition, array(<br />
&#8216;output_mode&#8217; =&gt; &#8216;file&#8217;,<br />
&#8216;output&#8217; =&gt; $dumpfile<br />
), MDB2_SCHEMA_DUMP_ALL);</p></blockquote>
<p>where $dumpfile is the <strong>file name</strong> you want your backup to go to.</p>
<p>The notion of <strong>schemas</strong> enables your application to <strong>seamless</strong> <strong>update</strong> the <strong>database</strong> across <strong>updates</strong>. The MDB2 can made <strong>automatic modifications</strong> of your table definitions to match the ones into the schema.</p>
<p>The library enables to use complex database functions, like <strong>transactions</strong>, to make your web applications <strong>secure</strong> and overcome stability problems when <strong>delicate procedures</strong> are performed, like the cart checkout on a e-commerce website, a money transfer, or any other situation where a <strong>power outage</strong> or a <strong>software interruption</strong> can lead to an <strong>partial update</strong>. Transactions <strong>group</strong> the <strong>operations</strong> so that any error will lead to a <strong>rollback</strong>, preserving the state your data was prior to updating.</p>
<p>If later in the process you want to <strong>change</strong> your database, for example from MySQL to Microsoft SQL server? Simply <strong>change</strong> your <strong>DSN</strong> from mysql to mssql and <strong>you are ready</strong>!</p>
<p>Did you like this article? Subscribe to my <em>transactional</em> feeds and leave a comment!</p>
<p class="fbconnect_share"><fb:share-button class="url" href="http://giancarlo.dimassa.net/2009/01/18/database-abstraction-in-php-with-mdb2/" /></p>]]></content:encoded>
			<wfw:commentRss>http://giancarlo.dimassa.net/2009/01/18/database-abstraction-in-php-with-mdb2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

