<?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>Storples Blog &#187; PHP</title>
	<atom:link href="http://blog.storples.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.storples.com</link>
	<description>Mmm, Digital(ish)</description>
	<lastBuildDate>Tue, 17 Aug 2010 17:53:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>PHP vs Perl</title>
		<link>http://blog.storples.com/2008/12/29/php-vs-perl/</link>
		<comments>http://blog.storples.com/2008/12/29/php-vs-perl/#comments</comments>
		<pubDate>Tue, 30 Dec 2008 03:49:09 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://blog.storples.com/?p=47</guid>
		<description><![CDATA[Being my first real post, I thought I would show something that I really benefitted me by moving to PHP from Perl, but that ultimately helped me whenever I maintained Perl code. In pulling data from a MySQL database for example, one might use the following PHP code: 1 2 3 4 5 6 7 [...]]]></description>
			<content:encoded><![CDATA[<p>Being my first real post, I thought I would show something that I really benefitted me by moving to PHP from Perl, but that ultimately helped me whenever I maintained Perl code.</p>
<p>In pulling data from a MySQL database for example, one might use the following PHP code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// MySQL database connection code previously defined</span>
<span style="color: #000088;">$statement</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;select first_name, last_name from users&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$user_data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$statement</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;First Name: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$user_data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'first_name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; Last Name: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$user_data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'last_name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>   
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Line 2: this is our MySQL query.<br />
Line 4: our while loop that goes through each row of returned MySQL date. mysql_fetch_assoc puts the MySQL data into an associative array.  (see array detail below).<br />
Line 6: here we echo everything out to the browser.</p>
<p>This would obviously pull all the first and last names from a users table, and print them out nice and neatly.  Notice that I used $user_data['first_name'] and user_data['first_name'] to pull the first name and last name, respectively.  User data is built like so:</p>
<pre>
Array
(
    [first_name] => John
    [last_name] => Doe
)
</pre>
<p>So when I code $user_data['first_name'], what I&#8217;ll get is &#8220;John&#8221;.</p>
<p>The same code in Perl might go something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># $dbh, or the MySQL database connection, was defined early in the code</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$statement</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;select * from users&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$statement</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;can't prepare the statement&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$rv</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;can't execute the query: $sth-&gt;errstr&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$user_data</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">fetchrow_hashref</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;First Name: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">$user_data</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'first_name'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">&quot; Last Name: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">$user_data</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'last_name'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>   
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Line 2: our MySQL query.<br />
Line 3: we <em>prepare</em> the statement for execution.<br />
Line 4: we execute the query, calling die if there is an error.<br />
Line 6: similar to the PHP while, we put the MySQL data into a Perl hash, very much like a PHP associative array.<br />
Line 8: here we print it all out.</p>
<p>The output of this code is exactly the same as the PHP code.  Sadly, it took me learning PHP to realize that you could even do this with MySQL data, and so &#8230; for many years I annoyingly coded my MySQL data in perl using indexes ($user_data[0] instead of $user_data->{&#8216;first_name&#8217;}).</p>
<p>Anyway, I hope this helps understand how to make Perl do what PHP does so easily.  Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.storples.com/2008/12/29/php-vs-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
