<?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>Professional PHP &#187; pdo</title>
	<atom:link href="http://www.procata.com/blog/archives/tag/pdo/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.procata.com/blog</link>
	<description>PHP Programming, Web Development, PHP Advocacy and PHP Best Practices.</description>
	<lastBuildDate>Fri, 10 Dec 2010 17:23:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PDO versus MDB2</title>
		<link>http://www.procata.com/blog/archives/2006/12/26/pdo-versus-mdb2/</link>
		<comments>http://www.procata.com/blog/archives/2006/12/26/pdo-versus-mdb2/#comments</comments>
		<pubDate>Tue, 26 Dec 2006 22:26:49 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[mdb2]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[pdo]]></category>
		<category><![CDATA[prepared-statement]]></category>

		<guid isPermaLink="false">http://www.procata.com/blog/archives/2006/12/26/pdo-versus-mdb2/</guid>
		<description><![CDATA[I was just putting together a small test program and I thought I would try using PDO.  I really haven&#8217;t done anything serious with PDO, just try it a couple times.  After recompiling PHP to include the mysql driver for PDO, I coded up the first version of my test program:
&#160;
$db = new [...]]]></description>
			<content:encoded><![CDATA[<p>I was just putting together a small test program and I thought I would try using PDO.  I really haven&#8217;t done anything serious with PDO, just try it a couple times.  After recompiling PHP to include the mysql driver for PDO, I coded up the first version of my test program:</p>
<p><pre class="php">&nbsp;
<span style="color: #0000ff;">$db</span> = <span style="color: #000000; font-weight: bold;">new</span> PDO<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'mysql:host=localhost;dbname=example'</span>, <span style="color: #ff0000;">'example'</span>, <span style="color: #ff0000;">'secret'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$tags</span> = <span style="color: #0000ff;">$db</span>-&gt;<span style="color: #006600;">prepare</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;
    SELECT 
        * 
    FROM 
        bookmark_tags, tags 
    WHERE 
        bookmark_tags.bookmark_id = ? AND 
        tags.id = bookmark_tags.tag_id 
    ORDER BY 
        tags.name&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$bookmarks</span> = <span style="color: #0000ff;">$db</span>-&gt;<span style="color: #006600;">prepare</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;SELECT * FROM bookmarks ORDER BY Title&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$bookmarks</span>-&gt;<span style="color: #006600;">execute</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$bookmark</span> = <span style="color: #0000ff;">$bookmarks</span>-&gt;<span style="color: #006600;">fetchObject</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;&lt;li&gt;&lt;a href='{$bookmark-&gt;url}'&gt;{$bookmark-&gt;title}&lt;/a&gt; &quot;</span>;
    
    <span style="color: #0000ff;">$tags</span>-&gt;<span style="color: #006600;">execute</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$bookmark</span>-&gt;<span style="color: #006600;">id</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$tag</span> = <span style="color: #0000ff;">$tags</span>-&gt;<span style="color: #006600;">fetchObject</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$tag</span>-&gt;<span style="color: #006600;">name</span>, <span style="color: #ff0000;">&quot; &quot;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;&lt;/li&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #66cc66;">&#125;</span>
<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;</pre></p>
<p>Unfortunately, this didn&#8217;t work and it took me a few minutes to figure out why.  Actually, I still don&#8217;t know exactly why it doesn&#8217;t work, but I did find a way to make it work: by using two separate connections, one for each prepared statement.  It doesn&#8217;t seem like you can have two active statements at the same time on the same connection.  I find this hard to believe, so I&#8217;m probably doing something wrong.</p>
<p>The other thing I didn&#8217;t care for with this PDO code is the non-standard method of iteration with the while loop.  Well, the while loop is perfectly standard if you are coming from the PHP 4 style functional DB APIs.  However, it doesn&#8217;t seem to fit in with the PHP 5 Iterator and foreach integration.  PDO doesn&#8217;t seem to provide a distinct result set object, or a method of iterating over a result set using the standard PHP Iterator interface.</p>
<p>Now, I can understand why this may be the case.  The PDO interface seems to be designed to bind to php variables.  Thats not going to work with the Iterator interface.  However, I am not using that mode and don&#8217;t want to use that mode.  It would be nice to be able to acquire an iterator for an example of use such as the one above without having to use fetchAll and ArrayIterator.</p>
<p>Using the Iterator style makes it easier for me to decouple my code from the data source and makes it easier to write test cases for that code.</p>
<p>I was a little bit disappointed.  So I moved on to MDB2 with the same code &#8230;</p>
<p><pre class="php">&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #ff0000;">'MDB2.php'</span>;
<span style="color: #b1b100;">require_once</span> <span style="color: #ff0000;">'MDB2/iterator.php'</span>;
&nbsp;
<span style="color: #0000ff;">$db</span> = MDB2::<span style="color: #006600;">connect</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;mysql://example:secret@localhost/example&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$tagLookup</span> = <span style="color: #0000ff;">$db</span>-&gt;<span style="color: #006600;">prepare</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;
    SELECT 
        * 
    FROM 
        bookmark_tags, tags 
    WHERE 
        bookmark_tags.bookmark_id = ? AND 
        tags.id = bookmark_tags.tag_id 
    ORDER BY 
        tags.name&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$bookmarkFinder</span> = <span style="color: #0000ff;">$db</span>-&gt;<span style="color: #006600;">prepare</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;SELECT * FROM bookmarks ORDER BY Title&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$bookmarks</span> = <span style="color: #000000; font-weight: bold;">new</span> MDB2_Iterator<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$bookmarkFinder</span>-&gt;<span style="color: #006600;">execute</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, MDB2_FETCHMODE_OBJECT<span style="color: #66cc66;">&#41;</span>;
    
<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;&lt;ul&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$bookmarks</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$bookmark</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;&lt;li&gt;&lt;a href='{$bookmark-&gt;url}'&gt;{$bookmark-&gt;title}&lt;/a&gt; &quot;</span>;
&nbsp;
    <span style="color: #0000ff;">$tags</span> = <span style="color: #000000; font-weight: bold;">new</span> MDB2_Iterator<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$tagLookup</span>-&gt;<span style="color: #006600;">execute</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$bookmark</span>-&gt;<span style="color: #006600;">id</span><span style="color: #66cc66;">&#41;</span>, MDB2_FETCHMODE_OBJECT<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$tags</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$tag</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$tag</span>-&gt;<span style="color: #006600;">name</span>, <span style="color: #ff0000;">&quot; &quot;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;&lt;/li&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #66cc66;">&#125;</span>
<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;&lt;/ul&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;</pre></p>
<p>I have about the same level of non-experience with MDB2 as with PDO, but my code worked perfectly on the first try and allowed me to use Iterator, which will be helpful to the next stage of my test.</p>
<p>I was a bit impressed.  I&#8217;ll see how well it handles the next stage of what I want to do.</p>
<p>(And yes, I know there is no error checking in the above code.)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.procata.com/blog/archives/2006/12/26/pdo-versus-mdb2/feed/</wfw:commentRss>
		<slash:comments>39</slash:comments>
		</item>
		<item>
		<title>PDO Design Evolution</title>
		<link>http://www.procata.com/blog/archives/2004/10/23/pdo/</link>
		<comments>http://www.procata.com/blog/archives/2004/10/23/pdo/#comments</comments>
		<pubDate>Sat, 23 Oct 2004 22:32:30 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Design]]></category>
		<category><![CDATA[api-design]]></category>
		<category><![CDATA[pdo]]></category>

		<guid isPermaLink="false">http://www.procata.com/blog/archives/2004/10/23/pdo/</guid>
		<description><![CDATA[Alan Knowles has an interesting post on PDO.
I really don&#8217;t know much about PDO, i haven&#8217;t used it yet.  Most of what I know comes from:
Wez Furlong&#8217;s first steps with PDO examples
Wez&#8217;s Oracle PDO Article
John Lim&#8217;s discussion of adodb and PDO
A sitepoint thread on PDO
I haven&#8217;t seen any API type docs yet.  I [...]]]></description>
			<content:encoded><![CDATA[<p>Alan Knowles has an interesting post <a href="http://blog.akbkhome.com/archives/55_PDO__Why_it_should_not_be_part_of_core_PHP.html">on PDO</a>.</p>
<p>I really don&#8217;t know much about PDO, i haven&#8217;t used it yet.  Most of what I know comes from:<br />
<a href="http://netevil.org/node.php?nid=44">Wez Furlong&#8217;s first steps with PDO examples</a><br />
<a href="http://www.oracle.com/technology/pub/articles/php_experts/otn_pdo_oracle5.html">Wez&#8217;s Oracle PDO Article</a><br />
<a href="http://phplens.com/phpeverywhere/?q=node/view/1">John Lim&#8217;s discussion of adodb and PDO</a><br />
<a href="http://www.sitepoint.com/forums/showthread.php?t=171535">A sitepoint thread on PDO</a><br />
I haven&#8217;t seen any API type docs yet.  I did download an older version of the PECL code and skim through it.</p>
<p>Alan has some interesting things to say.  I have to agree with him on using libgda as a back end.  Using a more mature, existing, debugged code base seems very reasonable to me.  Could libgda be used as the PDO back end while retaining the PDO front end API?</p>
<p>When I first looked at PDO, the parameter binding is one of the things that really jumped out at me.  It reminded me of register globals, a technique that didn&#8217;t necessarily turn out well and was painful to back out of.  Now that Alan says it, this technique is a bit more reminiscent of C than PHP.  As i understand its an optional way to use PDO.  A way that I would not use.</p>
<p>Wez <a href="http://blog.akbkhome.com/archives/55_PDO__Why_it_should_not_be_part_of_core_PHP.html#c186">comments</a>:</p>
<blockquote><p>
- copying C: I agree that bound parameters can lead to magic code. There are two things to keep in mind: a good coding style (with comments) is the best remedy for code that might be considered magic.
</p></blockquote>
<p>I have to strongly disagree on the coding style point.  Salting magic code with comments does not make it palatable.  The solution is to remove the magic.</p>
<p>I am not familiar enough with PDO to understand the iterator argument.  None of the examples I have seen actually use iterators.</p>
<p>What I would like to see out of a basic OO DB API in PHP is better support for translating the database record into an object.  For example, the fetch(PDO_FETCH_OBJ) method returns an anonymous object.  I would like a way for this method to construct an object of a particular class along with an initialization protocol such as <a href="http://www.php.net/manual/en/function.unserialize.php">unserialize</a> has with objects.</p>
<p>I do think that PDO should be a part of core PHP, but perhaps one with a more mature libgda backend and a more evolved PHP front end API, influenced by Alan&#8217;s feedback and DBDO efforts.</p>
<p>I have noticed that sometimes the API design in PHP tends to simply wrap underlying 3rd party C libraries, even when that design doesn&#8217;t make the most sense in PHP.  (for example the PHP wrapping of expat) PDO is a more of an attempt to design a PHP oriented API and then work the 3rd party libraries into the common design.  To me, this is a step forward.  However, the PDO API probably needs to go through a few iterations as Alan suggests before being frozen into core PHP.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.procata.com/blog/archives/2004/10/23/pdo/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

