<?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; prepared-statement</title>
	<atom:link href="http://www.procata.com/blog/archives/tag/prepared-statement/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>
	</channel>
</rss>

