<?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>code-diesel &#187; tools</title>
	<atom:link href="http://www.codediesel.com/tag/tools/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Automatically create PHP classes from MySQL</title>
		<link>http://www.codediesel.com/php/automatically-create-php-classes-from-mysql/</link>
		<comments>http://www.codediesel.com/php/automatically-create-php-classes-from-mysql/#comments</comments>
		<pubDate>Thu, 15 Sep 2011 06:37:54 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2912</guid>
		<description><![CDATA[Creating a database driven web application involves commonly used paradigms for data modification, which we commonly refer to as CRUD. Frameworks provides nice ORM wrappers to help the programmer. But for small projects frameworks can be an overkill. Still the programmer needs to design the basic database CRUD functionality, which can be quite tedious and [...]]]></description>
			<content:encoded><![CDATA[<p>Creating a database driven web application involves commonly used paradigms for data modification, which we commonly refer to as CRUD. Frameworks provides nice ORM wrappers to help the programmer. But for small projects frameworks can be an overkill. Still the programmer needs to design the basic database CRUD functionality, which can be quite tedious and repetitive. This is where auto database class generators can be helpful. If you have your database schema ready, you can automatically create the respective class wrappers for the tables.<br />
<span id="more-2912"></span><br />
I found two libraries which accomplish the task without much of an overhead.<br />
<a href="http://www.structy.com/home.html" title="structy" target="_blank">Structy</a> and <a href="https://github.com/stevenflesch/table2class" title="table2class" target="_blank">table2class</a>.</p>
<p>Take a sample database schema shown below.</p>

<div class="wp_codebox"><table><tr id="p29124"><td class="code" id="p2912code4"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #008000;">`user`</span> <span style="color: #FF00FF;">&#40;</span>
  <span style="color: #008000;">`username`</span> <span style="color: #999900; font-weight: bold;">varchar</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #990099; font-weight: bold;">default</span> <span style="color: #008000;">''</span><span style="color: #000033;">,</span>
  <span style="color: #008000;">`password`</span> <span style="color: #999900; font-weight: bold;">varchar</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #990099; font-weight: bold;">default</span> <span style="color: #008000;">''</span><span style="color: #000033;">,</span>
  <span style="color: #990099; font-weight: bold;">KEY</span> <span style="color: #008000;">`username`</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">`username`</span><span style="color: #FF00FF;">&#41;</span>
<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">ENGINE</span><span style="color: #CC0099;">=</span>MyISAM <span style="color: #990099; font-weight: bold;">DEFAULT</span> <span style="color: #FF9900; font-weight: bold;">CHARSET</span><span style="color: #CC0099;">=</span>latin1<span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>The following is a php class automatically generated by &#8216;table2class&#8217; from the above schema. The generated class file also includes additional support database initialization files. Note that some functions here may need modification to suit your particular application, but still, the major code required to get the application up and running is there. Any automation is better than nothing. And each such help to the programmer means faster project delivery time for the client.</p>

<div class="wp_codebox"><table><tr id="p29125"><td class="code" id="p2912code5"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/*************************************************************************
* Class Name:       user
* File Name:        class.user.php
* Generated:        Thursday, Sep 15, 2011 - 6:39:33 UTC
*  - for Table:     user
*   - in Database:  user
**************************************************************************/</span>
&nbsp;
<span style="color: #009933; font-style: italic;">// Files required by class:</span>
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;class.database.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">// Begin Class &quot;user&quot;</span>
<span style="color: #000000; font-weight: bold;">class</span> user <span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">// Variable declaration</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$username</span><span style="color: #339933;">;</span> <span style="color: #009933; font-style: italic;">// Primary Key</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$database</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">// Class Constructor</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Database<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">SetSettings</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;user&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;pass&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;user&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">// Class Destructor</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">// GET Functions</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getusername<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">// SET Functions</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setusername<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mValue</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mValue</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> select<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mID</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009933; font-style: italic;">// SELECT Function</span>
        <span style="color: #009933; font-style: italic;">// Execute SQL Query to get record.</span>
        <span style="color: #000088;">$sSQL</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM user WHERE username = <span style="color: #006699; font-weight: bold;">$mID</span>;&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$oResult</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sSQL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$oResult</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">result</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$oRow</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_object</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$oResult</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009933; font-style: italic;">// Assign results to class.</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$oRow</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span><span style="color: #339933;">;</span> <span style="color: #009933; font-style: italic;">// Primary Key</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> insert<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span> <span style="color: #009933; font-style: italic;">// Remove primary key value for insert</span>
        <span style="color: #000088;">$sSQL</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO user () VALUES ();&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$oResult</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sSQL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">lastinsertid</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> update<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mID</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$sSQL</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;UPDATE user SET (username = '<span style="color: #006699; font-weight: bold;">$this-&gt;username</span>') 
                 WHERE username = <span style="color: #006699; font-weight: bold;">$mID</span>;&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$oResult</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sSQL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> delete<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mID</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$sSQL</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;DELETE FROM user WHERE username = <span style="color: #006699; font-weight: bold;">$mID</span>;&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$oResult</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sSQL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #009933; font-style: italic;">// End Class &quot;user&quot;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>&#8216;Structy&#8217; provides a somewhat different flavor as you can see from the code class generated by the library. This is more complete than &#8216;table2class&#8217; but also a little complex. The &#8216;RET&#8217; constant you see in the code below is generated by the library and is automatically defined in the other support files.</p>

<div class="wp_codebox"><table><tr id="p29126"><td class="code" id="p2912code6"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**********************************************************************
user.class.php
Generated by STRUCTY 2011.09.15 08:33:41.
Copyright 2011 Structy, Frédéric Aebi. All rights reserved.
**********************************************************************/</span>
&nbsp;
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;USER&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;user&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> user <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$username</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$password</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setusername<span style="color: #009900;">&#40;</span><span style="color: #000088;">$pArg</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;0&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span><span style="color: #339933;">=</span><span style="color: #000088;">$pArg</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setpassword<span style="color: #009900;">&#40;</span><span style="color: #000088;">$pArg</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;0&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">password</span><span style="color: #339933;">=</span><span style="color: #000088;">$pArg</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getusername<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getpassword<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">password</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> readObject<span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</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: #000088;">$qry</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT *&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;FROM &quot;</span><span style="color: #339933;">.</span>USER<span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
        <span style="color: #000088;">$and</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;WHERE&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$qry</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$and</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;username = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
            <span style="color: #000088;">$and</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;AND&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$qry</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$and</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;password = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
            <span style="color: #000088;">$and</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;AND&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$record</span> <span style="color: #339933;">=</span> Database<span style="color: #339933;">::</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$qry</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$record</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$record</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$record</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setusername</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$record</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setpassword</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$record</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> readArray<span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</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: #000088;">$qry</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT *&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;FROM &quot;</span><span style="color: #339933;">.</span>USER<span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
        <span style="color: #000088;">$and</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;WHERE&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$qry</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$and</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;username = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
            <span style="color: #000088;">$and</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;AND&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$qry</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$and</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;password = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
            <span style="color: #000088;">$and</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;AND&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$recordset</span> <span style="color: #339933;">=</span> Database<span style="color: #339933;">::</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$qry</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$class_objects</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$recordset</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">,</span> <span style="color: #000088;">$record</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">each</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$recordset</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$class_object</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> user<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$class_object</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setusername</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$record</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$class_object</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setpassword</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$record</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$class_objects</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$class_object</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getusername</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$class_object</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$class_objects</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> insert<span style="color: #009900;">&#40;</span><span style="color: #000088;">$update</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$update</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$qry</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;UPDATE &quot;</span><span style="color: #339933;">.</span>USER<span style="color: #339933;">.</span>RET<span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;SET&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">.</span>
            <span style="color: #0000ff;">&quot;username = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getusername</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;',&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">.</span>
            <span style="color: #0000ff;">&quot;password = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getpassword</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">.</span>
    <span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
&nbsp;
            Database<span style="color: #339933;">::</span><span style="color: #004000;">insert</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$qry</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$qry</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO &quot;</span><span style="color: #339933;">.</span>USER<span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; (&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">.</span>
            <span style="color: #0000ff;">&quot;username, password&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">.</span>
            <span style="color: #0000ff;">&quot;) VALUES (&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">.</span>
            <span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getusername</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;',&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">.</span>
            <span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getpassword</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">.</span>
            <span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
&nbsp;
            Database<span style="color: #339933;">::</span><span style="color: #004000;">insert</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$qry</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> delete<span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</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: #000088;">$qry</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;DELETE&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;FROM &quot;</span><span style="color: #339933;">.</span>USER<span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
        <span style="color: #000088;">$and</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;WHERE&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$qry</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$and</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;username = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
            <span style="color: #000088;">$and</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;AND&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$qry</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$and</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;password = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
            <span style="color: #000088;">$and</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;AND&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        Database<span style="color: #339933;">::</span><span style="color: #004000;">delete</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$qry</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/automatically-create-php-classes-from-mysql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Local email testing of applications</title>
		<link>http://www.codediesel.com/tools/local-email-testing-of-applications/</link>
		<comments>http://www.codediesel.com/tools/local-email-testing-of-applications/#comments</comments>
		<pubDate>Wed, 07 Oct 2009 04:41:25 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[tools]]></category>
		<category><![CDATA[email]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=1697</guid>
		<description><![CDATA[local testing of email applications]]></description>
			<content:encoded><![CDATA[<p>E-mails are a pervasive element of most web applications. But many times testing and debugging emails can become a hassle when many of them are involved. Most of the time you just want to be sure that the email part of your application is working fine, without flooding your mail account with test mails.<br />
<span id="more-1697"></span><br />
<a target="_blank" href="http://invalidlogic.com/papercut/">Papercut</a> is a simplified SMTP server designed to help you receive emails from your local applications but without sending them out. It doesn&#8217;t even care if the email address you sent is valid or not. It just takes the email you sent and displays and logs it in a window for you to see if it is correct. It can be quite useful if you just need to make sure that the email part of your applications is working fine.</p>
<h4>Installation</h4>
<p>Papercut is a Windows application requiring .NET Framework 3.5 . Thankfully no installation is required as the application can be run directly. Upon activation it sits in the system tray and pops a message when a email is received from a local web application.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2009/10/papercut1.png"><img style="border:1px solid #000;" src="http://www.codediesel.com/wp-content/uploads/2009/10/papercut1.png" alt="papercut1" title="papercut1" width="238" height="62" class="aligncenter size-full wp-image-1701" /></a></p>
<p>The messages can then be viewed in the Papercut window.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2009/10/papercut2.png"><img src="http://www.codediesel.com/wp-content/uploads/2009/10/papercut2.png" alt="papercut2" title="papercut2" width="490" height="282" class="aligncenter size-full wp-image-1705" /></a></p>
<p>In all a simple and useful tool for any web developer.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/tools/local-email-testing-of-applications/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Adminer &#8211; a fast MySQL administration tool</title>
		<link>http://www.codediesel.com/mysql/adminer-a-fast-mysql-administrator-tool/</link>
		<comments>http://www.codediesel.com/mysql/adminer-a-fast-mysql-administrator-tool/#comments</comments>
		<pubDate>Tue, 18 Aug 2009 10:58:51 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=1413</guid>
		<description><![CDATA[mysql administration program, adminer]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.codediesel.com/wp-content/uploads/2009/08/adminer1.gif"><img src="http://www.codediesel.com/wp-content/uploads/2009/08/adminer1.gif" alt="adminer1" title="adminer1" width="64" height="68" class="alignleft size-full wp-image-1435" /></a>Before you go ahead and think that you don&#8217;t need yet another MySQL management tool, I think you should at least take a quick look at this one. <a target="_blank" href="http://www.adminer.org/en/">Adminer</a> is a single file (yes! everything in a single php file) tool to manage all your MySQL related tasks. All you have to do is upload the adminer.php file to your server and voila! you are ready to go. No messing around with huge software&#8217;s to manage your database. Weighing only 170KB, its easy to upload to any server. A demo of the same is included on the <a href="http://www.adminer.org/en/">Adminer</a> site.<br />
<span id="more-1413"></span><br />
Of course other tools such as phpMyadmin, MySQL Workbench, SQLyog MySQL GUI have their advantages with the various advanced features they provide, but if you need a quick way to manage your database with all of the standard tasks, then Adminer will surely suit your purpose. </p>
<p>Let us take a quick look at some features Adminer provides:</p>
<p># Connect to a database server with username and password<br />
# Select an existing database or create a new one<br />
# List fields, indexes, foreign keys and triggers of table<br />
# Change name, engine, collation, auto_increment and comment of table<br />
# Alter name, type, collation, comment and default values of columns<br />
# Add and drop tables and columns<br />
# Create, alter, drop and search by indexes including fulltext<br />
# Create, alter, drop and link lists by foreign keys<br />
# Create, alter, drop and select from views<br />
# Create, alter, drop and call stored procedures and functions<br />
# Create, alter and drop triggers<br />
# List data in tables with search, aggregate, sort and limit results<br />
# Insert new records, update and delete the existing ones<br />
# Supports all data types, blobs through file transfer<br />
# Execute any SQL command from a text field or a file<br />
# Export table structure, data, views, routines, databases to SQL or CSV<br />
# Print database schema connected by foreign keys<br />
# Show processes and kill them<br />
# Display users and rights and change them<br />
# Display variables with links to documentation<br />
# Manage events and table partitions (MySQL 5.1)</p>
<p>I don&#8217;t know about you but all this features will get me through the day.</p>
<h4>Compiled files</h4>
<p>But the most interesting idea I liked about Adminer is that if you download the source zip you will see that the software is actually composed of different files (which also are executable). The root folder contains a <em>compile.php</em> script that when executed combines all the required files in a single <em>adminer.php</em> file. During compilation all the php files are compressed (by removing the spaces), the images are base64 encoded and the javascript minified. In the true sense of the word the files are really not compiled but rather compressed and squeezed together in a single file.</p>
<h4>Skining</h4>
<p>You can also add your own skins to the application by creating a <em>adminer.css</em> file and putting it in the directory where adminer.php is located.</p>
<h4>Extensions</h4>
<p>Adminer also allows to to override some default functionality by writing your own custom code. Although I&#8217;ve not played much with this feature yet.</p>
<h4>In conclusion</h4>
<p>A fine product, which I find immensely helpful when I need to quickly perform some database task on a clients server.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/mysql/adminer-a-fast-mysql-administrator-tool/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Detecting duplicate code in PHP files</title>
		<link>http://www.codediesel.com/tools/detecting-duplicate-code-in-php-files/</link>
		<comments>http://www.codediesel.com/tools/detecting-duplicate-code-in-php-files/#comments</comments>
		<pubDate>Tue, 07 Apr 2009 04:33:25 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[testing]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=625</guid>
		<description><![CDATA[Duplicated code in projects is a frequent thing and also the one ripe for factoring out in a new class or function. Cut/Paste coding is a common development practice among programmers, a lot of which can lead to code size increase and maintenance nightmares. PHPCPD (php copy paste detector) is a PEAR tool that makes [...]]]></description>
			<content:encoded><![CDATA[<p>Duplicated code in projects is a frequent thing and also the one ripe for factoring out in a new class or function. Cut/Paste coding is a common development practice among programmers, a lot of which can lead to code size increase and maintenance nightmares. <a target="_blank" href="http://github.com/sebastianbergmann/phpcpd/tree/master">PHPCPD</a> (php copy paste detector) is a PEAR tool that makes it easier to detect duplicate code in php projects. Below is a short tutorial on the PHPCPD package.<br />
<span id="more-625"></span></p>
<p><strong>1. Installing phpcpd</strong><br />
We will be using the PEAR installer for this purpose. First the PEAR channel that is used to distribute phpcpd needs to be registered with the local PEAR environment. This tells PEAR from where the install files should be downloaded.</p>

<div class="wp_codebox"><table><tr id="p62513"><td class="code" id="p625code13"><pre class="dos" style="font-family:monospace;">c:\&gt; pear channel-discover pear.phpunit.de
Adding Channel &quot;pear.phpunit.de&quot; succeeded
Discovery of channel &quot;pear.phpunit.de&quot; succeeded</pre></td></tr></table></div>

<p>After this is done the PEAR installer is ready to install phpcpd.</p>

<div class="wp_codebox"><table><tr id="p62514"><td class="code" id="p625code14"><pre class="dos" style="font-family:monospace;">c:\&gt; pear install phpunit/phpcpd
downloading phpcpd-1.1.1.tgz ...
Starting to download phpcpd-1.1.1.tgz <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span>,078 bytes<span style="color: #66cc66;">&#41;</span>
.....done: <span style="color: #cc66cc;">8</span>,078 bytes
install ok: channel://pear.phpunit.de/phpcpd-1.1.1</pre></td></tr></table></div>

<p>Post installation you will find the PHPCPD source files in the PEAR directory.</p>
<p><strong>2. Running your first check.</strong><br />
Here is our first check on the <i>admin</i> sub directory of a project with the results. You can check for duplicate code in a individual file or a directory.</p>

<div class="wp_codebox"><table><tr id="p62515"><td class="code" id="p625code15"><pre class="dos" style="font-family:monospace;">c:\localhost\project&gt; phpcpd ./admin
phpcpd 1.1.1 by Sebastian Bergmann.
&nbsp;
Found <span style="color: #cc66cc;">3</span> exact clones with <span style="color: #cc66cc;">50</span> duplicated lines <span style="color: #00b100; font-weight: bold;">in</span> <span style="color: #cc66cc;">6</span> files:
&nbsp;
  - .messages.php:<span style="color: #cc66cc;">95</span>-<span style="color: #cc66cc;">105</span>
    .messagesgroup.php:<span style="color: #cc66cc;">112</span>-<span style="color: #cc66cc;">122</span>
&nbsp;
  - .viewschedules.php:<span style="color: #cc66cc;">14</span>-<span style="color: #cc66cc;">23</span>
    .tutorbookings.php:<span style="color: #cc66cc;">14</span>-<span style="color: #cc66cc;">23</span>
&nbsp;
  - .ampieexport.php:<span style="color: #cc66cc;">4</span>-<span style="color: #cc66cc;">35</span>
    .amcolumnexport.php:<span style="color: #cc66cc;">4</span>-<span style="color: #cc66cc;">35</span>
&nbsp;
<span style="color: #cc66cc;">0.35</span><span style="color: #33cc33;">%</span> duplicated lines out of <span style="color: #cc66cc;">14456</span> total lines of code.</pre></td></tr></table></div>

<p><strong>3. Options</strong><br />
By default phpcpd will search for a minimum of 5 identical lines and 70 identical tokens. So if there are less than 5 duplicate lines in the code or less than 70 identical tokens they will be ignored. To override this you can use the <em>&#8211;min-lines</em> and <em>&#8211;min-tokens</em> switch as below.</p>

<div class="wp_codebox"><table><tr id="p62516"><td class="code" id="p625code16"><pre class="dos" style="font-family:monospace;">c:\localhost\project&gt; phpcpd --min-lines <span style="color: #cc66cc;">4</span> --min-tokens <span style="color: #cc66cc;">40</span> ./admin
phpcpd 1.1.1 by Sebastian Bergmann.
&nbsp;
Found <span style="color: #cc66cc;">9</span> exact clones with <span style="color: #cc66cc;">187</span> duplicated lines <span style="color: #00b100; font-weight: bold;">in</span> <span style="color: #cc66cc;">14</span> files:
&nbsp;
  - .actionaction.updatestudent.php:<span style="color: #cc66cc;">15</span>-<span style="color: #cc66cc;">27</span>
    .actionaction.updatetutor.php:<span style="color: #cc66cc;">15</span>-<span style="color: #cc66cc;">27</span>
&nbsp;
  - .adminFunctions.php:<span style="color: #cc66cc;">91</span>-<span style="color: #cc66cc;">98</span>
    .adminFunctions.php:<span style="color: #cc66cc;">124</span>-<span style="color: #cc66cc;">131</span>
&nbsp;
  - .messages.php:<span style="color: #cc66cc;">95</span>-<span style="color: #cc66cc;">118</span>
    .messagesgroup.php:<span style="color: #cc66cc;">112</span>-<span style="color: #cc66cc;">135</span>
&nbsp;
  - .viewschedules.php:<span style="color: #cc66cc;">14</span>-<span style="color: #cc66cc;">84</span>
    .tutorbookings.php:<span style="color: #cc66cc;">14</span>-<span style="color: #cc66cc;">84</span>
&nbsp;
  - .viewschedules.php:<span style="color: #cc66cc;">167</span>-<span style="color: #cc66cc;">185</span>
    .tutorbookings.php:<span style="color: #cc66cc;">145</span>-<span style="color: #cc66cc;">163</span>
&nbsp;
  - .tutors.php:<span style="color: #cc66cc;">14</span>-<span style="color: #cc66cc;">20</span>
    .dimdim.php:<span style="color: #cc66cc;">14</span>-<span style="color: #cc66cc;">20</span>
&nbsp;
  - .ampieexport.php:<span style="color: #cc66cc;">4</span>-<span style="color: #cc66cc;">44</span>
    .amcolumnexport.php:<span style="color: #cc66cc;">4</span>-<span style="color: #cc66cc;">44</span>
&nbsp;
  - .geoipgeoip.inc.php:<span style="color: #cc66cc;">236</span>-<span style="color: #cc66cc;">241</span>
    .geoipgeoip.inc.php:<span style="color: #cc66cc;">272</span>-<span style="color: #cc66cc;">277</span>
&nbsp;
  - .studentschedule.php:<span style="color: #cc66cc;">14</span>-<span style="color: #cc66cc;">20</span>
    .<span style="color: #b1b100; font-weight: bold;">Copy</span> of onetomanyschedule.php:<span style="color: #cc66cc;">14</span>-<span style="color: #cc66cc;">20</span>
&nbsp;
<span style="color: #cc66cc;">1.29</span><span style="color: #33cc33;">%</span> duplicated lines out of <span style="color: #cc66cc;">14457</span> total lines of code.</pre></td></tr></table></div>

<p>The report generated by phpcpd can also be exported to a <a target="_blank" href="http://pmd.sourceforge.net/">PMD-CPD</a> xml format. The following scans the <em>admin</em> directory and saves the report in the <em>projectPhpcpd.xml</em> file.</p>

<div class="wp_codebox"><table><tr id="p62517"><td class="code" id="p625code17"><pre class="dos" style="font-family:monospace;">c:\localhost\project&gt; phpcpd --log-pmd projectPhpcpd.xml ./admin</pre></td></tr></table></div>

<p>Most of the php source files have the .php extension and phpcpd uses this by default when comparing files. To add other extensions to the list you can use the <i>&#8211;suffixes</i> option, which takes a comma separated list of extension names.</p>

<div class="wp_codebox"><table><tr id="p62518"><td class="code" id="p625code18"><pre class="dos" style="font-family:monospace;">c:\localhost\project&gt; phpcpd --suffixes php,php5 ./admin</pre></td></tr></table></div>

<p><strong>Concluding thoughts</strong><br />
There is also a Java program called <a target="_blank" href="http://pmd.sourceforge.net/cpd.html">PMD</a> which can detect duplicate code. But the main advantage of a PEAR package is that you can integrate it in your project itself or use it with <a target="_blank" href="http://phpundercontrol.org/about.html">phpUnderControl</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/tools/detecting-duplicate-code-in-php-files/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Checking coding standards with PHP_Codesniffer</title>
		<link>http://www.codediesel.com/tools/checking-coding-standards-with-php_codesniffer/</link>
		<comments>http://www.codediesel.com/tools/checking-coding-standards-with-php_codesniffer/#comments</comments>
		<pubDate>Mon, 30 Mar 2009 11:23:19 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[tools]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[standards]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=589</guid>
		<description><![CDATA[One of the important factors in developing a successful software product is the application of coding standards. And by successful I mean a product that is easy to develop, modify and maintain. Every developer likes to code in a particular style: in the way he declares variables or function blocks, the number of indentation spaces, [...]]]></description>
			<content:encoded><![CDATA[<p>One of the important factors in developing a successful software product is the application of coding standards. And by successful I mean a product that is easy to develop, modify and maintain. Every developer likes to code in a particular style: in the way he declares variables or function blocks, the number of indentation spaces, commenting style etc. Giving every developer in a team complete freedom to use whatever style he likes will very likely result in a code that is hard to understand, modify and maintain. Even though every coding standard has its good points and bad; the important thing is to try to adhere to a particular one.<br />
<span id="more-589"></span><br />
A coding standard basically tells developers in what style they must write their code. If programmers work in a team than a coding standard ensures that each will be able to read the others code without any effort. New programmers drafted in the team will be quick to pick the coding style and continue the work rather than waste time deciphering each persons individual style. </p>
<p>But writing a coding standard document and making your team or yourself (if you are a individual programmer) stick to it is a completely different matter. Most programmers usually slip into their own style of coding after some time. A tool which scans your source code and checks it against a standard would be quite a help. <a target="_blank" href="http://pear.php.net/manual/en/package.php.php-codesniffer.php">PHP_CodeSniffer</a> is one such tool.</p>
<p>PHP_CodeSniffer is a PHP5 script that &#8220;sniffs&#8221; PHP, JavaScript and CSS files to detect violations of a defined coding standard.</p>
<p><strong>1. Installing PHP_Codesniffer</strong><br />
The easy way to install is with the PEAR installer.</p>

<div class="wp_codebox"><table><tr id="p58928"><td class="code" id="p589code28"><pre class="php" style="font-family:monospace;">c<span style="color: #339933;">:</span>\<span style="color: #339933;">&gt;</span>pear install PHP_Codesniffer</pre></td></tr></table></div>

<p>By default a number of coding standards are installed against which you can check your code. You can find the installed standards by:</p>

<div class="wp_codebox"><table><tr id="p58929"><td class="code" id="p589code29"><pre class="php" style="font-family:monospace;">c<span style="color: #339933;">:</span>\<span style="color: #339933;">&gt;</span>phpcs <span style="color: #339933;">-</span>i
The installed coding standards are MySource<span style="color: #339933;">,</span> PEAR<span style="color: #339933;">,</span> PHPCS<span style="color: #339933;">,</span> Squiz and Zend</pre></td></tr></table></div>

<p><strong>2. Checking source file or directory</strong><br />
You can check a source file or directory against a particular standard. By default PHP_Codesniffer will use the PEAR coding standard. We will run the script on the following code using the default PEAR standard.</p>

<div class="wp_codebox"><table><tr id="p58930"><td class="code" id="p589code30"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/* Hello.php */</span>
<span style="color: #000000; font-weight: bold;">function</span> hello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;Hello World!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> hello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>


<div class="wp_codebox"><table><tr id="p58931"><td class="code" id="p589code31"><pre class="php" style="font-family:monospace;">c<span style="color: #339933;">:</span>\localhost\test<span style="color: #339933;">&gt;</span>phpcs hello<span style="color: #339933;">.</span>php</pre></td></tr></table></div>

<p>Which will return the following report.</p>

<div class="wp_codebox"><table><tr id="p58932"><td class="code" id="p589code32"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">FILE</span><span style="color: #339933;">:</span> c<span style="color: #339933;">:</span>\localhost\test\hello<span style="color: #339933;">.</span>php
<span style="color: #339933;">--------------------------------------------------------------</span>
FOUND <span style="color: #cc66cc;">3</span> ERROR<span style="color: #009900;">&#40;</span>S<span style="color: #009900;">&#41;</span> AND <span style="color: #cc66cc;">0</span> WARNING<span style="color: #009900;">&#40;</span>S<span style="color: #009900;">&#41;</span> AFFECTING <span style="color: #cc66cc;">2</span> LINE<span style="color: #009900;">&#40;</span>S<span style="color: #009900;">&#41;</span>
<span style="color: #339933;">--------------------------------------------------------------</span>
 <span style="color: #cc66cc;">2</span> <span style="color: #339933;">|</span> ERROR <span style="color: #339933;">|</span> Missing <span style="color: #990000;">file</span> doc comment
 <span style="color: #cc66cc;">3</span> <span style="color: #339933;">|</span> ERROR <span style="color: #339933;">|</span> Missing <span style="color: #000000; font-weight: bold;">function</span> doc comment
 <span style="color: #cc66cc;">3</span> <span style="color: #339933;">|</span> ERROR <span style="color: #339933;">|</span> Opening brace should be on a <span style="color: #000000; font-weight: bold;">new</span> line
<span style="color: #339933;">--------------------------------------------------------------</span></pre></td></tr></table></div>

<p>Using the Zend standard on the same code will return the following.</p>

<div class="wp_codebox"><table><tr id="p58933"><td class="code" id="p589code33"><pre class="php" style="font-family:monospace;">c<span style="color: #339933;">:</span>\localhost\test<span style="color: #339933;">&gt;</span>phpcs <span style="color: #339933;">--</span>standard<span style="color: #339933;">=</span>zend hello<span style="color: #339933;">.</span>php</pre></td></tr></table></div>


<div class="wp_codebox"><table><tr id="p58934"><td class="code" id="p589code34"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">FILE</span><span style="color: #339933;">:</span> c<span style="color: #339933;">:</span>\localhost\test\hello<span style="color: #339933;">.</span>php
<span style="color: #339933;">-------------------------------------------------------------------</span>
FOUND <span style="color: #cc66cc;">1</span> ERROR<span style="color: #009900;">&#40;</span>S<span style="color: #009900;">&#41;</span> AND <span style="color: #cc66cc;">1</span> WARNING<span style="color: #009900;">&#40;</span>S<span style="color: #009900;">&#41;</span> AFFECTING <span style="color: #cc66cc;">2</span> LINE<span style="color: #009900;">&#40;</span>S<span style="color: #009900;">&#41;</span>
<span style="color: #339933;">-------------------------------------------------------------------</span>
 <span style="color: #cc66cc;">3</span> <span style="color: #339933;">|</span> WARNING <span style="color: #339933;">|</span> Consider putting <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0000ff;">&quot;hello&quot;</span> in a static <span style="color: #000000; font-weight: bold;">class</span>
 <span style="color: #cc66cc;">9</span> <span style="color: #339933;">|</span> ERROR   <span style="color: #339933;">|</span> A closing tag is not permitted at the <span style="color: #990000;">end</span> of a PHP <span style="color: #990000;">file</span>
<span style="color: #339933;">--------------------------------------------------------------------</span></pre></td></tr></table></div>

<p>You can run the script on a directory as below with a summary report option.</p>

<div class="wp_codebox"><table><tr id="p58935"><td class="code" id="p589code35"><pre class="php" style="font-family:monospace;">c<span style="color: #339933;">:</span>\localhost\test<span style="color: #339933;">&gt;</span>phpcs <span style="color: #339933;">--</span>report<span style="color: #339933;">=</span>summary <span style="color: #339933;">./</span>ds</pre></td></tr></table></div>


<div class="wp_codebox"><table><tr id="p58936"><td class="code" id="p589code36"><pre class="php" style="font-family:monospace;">&nbsp;
PHP CODE SNIFFER REPORT SUMMARY
<span style="color: #339933;">----------------------------------------------------------------</span>
<span style="color: #990000;">FILE</span>                                        ERRORS  WARNINGS
<span style="color: #339933;">----------------------------------------------------------------</span>
c<span style="color: #339933;">:</span>\localhost\test\ds\link_list<span style="color: #339933;">.</span>php             <span style="color: #cc66cc;">8</span>       <span style="color: #cc66cc;">4</span>
c<span style="color: #339933;">:</span>\localhost\test\ds\linklist<span style="color: #339933;">.</span><span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">.</span>php        <span style="color: #cc66cc;">64</span>      <span style="color: #cc66cc;">30</span>
c<span style="color: #339933;">:</span>\localhost\test\ds\LinkListTest<span style="color: #339933;">.</span>php          <span style="color: #cc66cc;">7</span>       <span style="color: #cc66cc;">0</span>
c<span style="color: #339933;">:</span>\localhost\test\ds\hello<span style="color: #339933;">.</span>php                 <span style="color: #cc66cc;">3</span>       <span style="color: #cc66cc;">0</span>
<span style="color: #339933;">----------------------------------------------------------------</span>
A TOTAL OF <span style="color: #cc66cc;">82</span> ERROR<span style="color: #009900;">&#40;</span>S<span style="color: #009900;">&#41;</span> AND <span style="color: #cc66cc;">34</span> WARNING<span style="color: #009900;">&#40;</span>S<span style="color: #009900;">&#41;</span> WERE FOUND IN <span style="color: #cc66cc;">4</span> <span style="color: #990000;">FILE</span><span style="color: #009900;">&#40;</span>S<span style="color: #009900;">&#41;</span>
<span style="color: #339933;">----------------------------------------------------------------</span></pre></td></tr></table></div>

<p><strong>3. Your own coding standards</strong><br />
You can even create your own custom coding standards and add it to PHP_Codesniffer. Coding standard files in PHP_CodeSniffer are a collection of <em>sniff</em> files. Each sniff file checks one particular part of a coding standard only. The <a target="_blank" href="http://pear.php.net/manual/en/package.php.php-codesniffer.coding-standard-tutorial.php">Coding Standard Tutorial</a> explains it in quite a detail.</p>
<p><strong>4. Subversion integration</strong><br />
PHP_Codesniffer also integrates with Subversion, which ensures that developers do not commit code that violates the coding standard. Instead developers are presented with the list of errors they need to correct before committing to a repository.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/tools/checking-coding-standards-with-php_codesniffer/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Web scraping tutorial</title>
		<link>http://www.codediesel.com/php/web-scraping-in-php-tutorial/</link>
		<comments>http://www.codediesel.com/php/web-scraping-in-php-tutorial/#comments</comments>
		<pubDate>Sat, 07 Mar 2009 18:02:47 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[tools]]></category>
		<category><![CDATA[scraping]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=347</guid>
		<description><![CDATA[There are three ways to access a website data. One is through a browser, the other is using a API (if the site provides one) and the last by parsing the web pages through code. The last one also known as Web Scraping is a technique of extracting information from websites using specially coded programs. [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-459" style="border: none;" title="scrape" src="http://www.codediesel.com/wp-content/uploads/2009/03/scrape.jpeg" alt="scrape" width="100" height="79" />There are three ways to access a website data. One is through a browser, the other is using a API (if the site provides one) and the last by parsing the web pages through code. The last one also known as <em>Web Scraping</em> is a technique of extracting information from websites using specially coded programs.</p>
<p><span id="more-347"></span></p>
<p>In this post we will take a quick look at writing a simple scraperusing the <a href="http://simplehtmldom.sourceforge.net/" target="_blank">simplehtmldom</a> library. But before we continue a word of caution:</p>
<p style="padding: 5px; background-color: #D1B1B1; border: 1px solid #000;">Writing screen scrapers and spiders that consume large amounts of bandwidth, guess passwords, grab information from a site and use it somewhere else may well be a violation of someone&#8217;s rights and will eventually land  you in trouble. Before writing  a screen scraper first see if the website offers an RSS feed or an API for the data you are looking. If not and you have to use a scraper, first check the websites policies regarding automated tools before proceeding.</p>
<p>Now that we have got all the legalities  out of the way, lets start with the examples.</p>
<p><strong>1. Installing simplehtmldom.</strong><br />
Simplehtmldom is a PHP library that facilitates the process of creating web scrapers. It is a HTML DOM parser written in PHP5 that let you manipulate HTML in a quick and easy way. It is a wonderful library that does away with the messy details of regular expressions and uses CSS selector style DOM access like those found in jQuery.</p>
<p>First download the library from <a title="simplehtmldom download" href="http://sourceforge.net/projects/simplehtmldom/" target="_blank">sourceforge</a>.  Unzip the library in you PHP includes directory or a directory where you will be testing the code.</p>
<p><strong>2. Installing FireBug.</strong><br />
I assume you have FireBug already installed, if not head over <a title="firebug installation" href="https://addons.mozilla.org/en-US/firefox/addon/1843" target="_blank">here</a> for the installation. You will soon see why it is required.</p>
<p><strong>3. Writing our first scraper.</strong><br />
Now that we are ready with the tools, lets write our first web scraper. For our initial idea let us see how to grab the sponsored links section from a google search page.</p>
<p><img class="size-full wp-image-353" style="border: 1px solid #000;" title="google_search1" src="http://www.codediesel.com/wp-content/uploads/2009/02/google_search1.gif" alt="google search" width="530" height="260" /></p>
<p>Before we can retrieve the required data we need to know the HTML structure of the page so that we can know precisely where the required information is located. For this purpose FireBug is quite a handy tool. Open FireBug and click on the inspect button to check the Dom structure of the page. A sample image is shown below.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2009/02/firebug.gif"><img class="size-medium wp-image-357" style="border: 1px solid #000;" title="firebug" src="http://www.codediesel.com/wp-content/uploads/2009/02/firebug.gif" alt="firebug" width="500" height="262" /></a></p>
<p>As can be seen from the FireBug pane the whole sponsored links section is inside a &lt;td&gt; tag with a id named &#8216;rhsline&#8217;. The following is a Dom representation of the &#8216;rhsline&#8217; node.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2009/02/rhsline1.gif"><img class="aligncenter size-full wp-image-369" style="border: 1px solid #000;" title="rhsline1" src="http://www.codediesel.com/wp-content/uploads/2009/02/rhsline1.gif" alt="rhsline1" width="559" height="252" /></a></p>
<p>The &#8216;li&#8217; tags contain the actual data we are after.  With this little information we can use the following code to read all the sponsored links.</p>

<div class="wp_codebox"><table><tr id="p34742"><td class="code" id="p347code42"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span>  <span style="color: #000088;">$html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'td[id=rhsline]'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">children</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The complete source is given below.</p>

<div class="wp_codebox"><table><tr id="p34743"><td class="code" id="p347code43"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* update your path accordingly */</span>
<span style="color: #000000; font-weight: bold;">include_once</span> <span style="color: #0000ff;">'libs/simplehtmldom/simple_html_dom.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$search_term</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;mobiles&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://www.google.co.in/search?hl=en&amp;q=<span style="color: #006699; font-weight: bold;">{$search_term}</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$html</span> <span style="color: #339933;">=</span> file_get_html<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/*
Get all table rows having the id attribute named 'rhsline'.
As the list of sponsored links is in the 'ol' tag; as can be
seen from the DOM tree above; we use the 'children' function
on the $data object to get the sponsored links.
*/</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span>  <span style="color: #000088;">$html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'td[id=rhsline]'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/*
  Make sure that sponsors ads are available,
  Some keywords do not have sponsor ads.
*/</span>
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">children</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>In the next example we will grab the list of contents from the latest <a href="http://www.wired.com/wired/" target="_blank">Wired</a> magazine issue.</p>

<div class="wp_codebox"><table><tr id="p34744"><td class="code" id="p347code44"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000088;">$ret</span> <span style="color: #339933;">=</span>  <span style="color: #000088;">$html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'div[id=this_month] div[class=story]'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ret</span> <span style="color: #000000; font-weight: bold;">as</span> <span style="color: #000088;">$story</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$story</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'a'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This will return all the content section links from the page. To just return the links as text we can use the &#8216;plaintext&#8217; modifier.</p>

<div class="wp_codebox"><table><tr id="p34745"><td class="code" id="p347code45"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$story</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'a'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">plaintext</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The complete source is given below.</p>

<div class="wp_codebox"><table><tr id="p34746"><td class="code" id="p347code46"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* update your path accordingly */</span>
<span style="color: #000000; font-weight: bold;">include_once</span> <span style="color: #0000ff;">'libs/simplehtmldom/simple_html_dom.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://www.wired.com/wired/&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$html</span> <span style="color: #339933;">=</span> file_get_html<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$ret</span> <span style="color: #339933;">=</span>  <span style="color: #000088;">$html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'div[id=this_month] div[class=story]'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ret</span> <span style="color: #000000; font-weight: bold;">as</span> <span style="color: #000088;">$story</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$story</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'a'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">plaintext</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Now that you have seen how simple it is to scrape a web page you can read the simplehtmldom <a href="http://simplehtmldom.sourceforge.net/manual.htm" target="_blank">manual</a> to get more details on the various functions available in the library and start creating your own web scrapers.</p>
<blockquote><p>
I&#8217;m available for various kinds of web scrapping projects.<br />
Contact me with details to &#8216;metapix [at] gmail.com&#8217; for price quote.
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/web-scraping-in-php-tutorial/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Browser based SQL schema designer</title>
		<link>http://www.codediesel.com/mysql/browser-based-sql-schema-designer/</link>
		<comments>http://www.codediesel.com/mysql/browser-based-sql-schema-designer/#comments</comments>
		<pubDate>Tue, 03 Feb 2009 11:11:29 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=253</guid>
		<description><![CDATA[WWW SQL Designer is a free database designer. It allows developers to create database schemas using a graphical interface. The designs can be further saved/loaded and exported to SQL scripts. The software supports a number of databases and languages. This tool allows you to draw and create E-R diagrams in your browser using Javascript without [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://code.google.com/p/wwwsqldesigner/">WWW SQL Designer</a> is a free database designer. It allows developers to create database schemas using a graphical interface. The designs can be further saved/loaded and exported to SQL scripts. The software supports a number of databases and languages.<br />
<span id="more-253"></span><br />
<a href="http://www.codediesel.com/wp-content/uploads/2009/02/image11.gif" target="_self"><img class="aligncenter size-full wp-image-267" style="border: 1px solid #c0c0c0;" title="image11" src="http://www.codediesel.com/wp-content/uploads/2009/02/image11.gif" alt="image11" width="973" height="533" /></a></p>
<p>This tool allows you to draw and create E-R diagrams in your browser using Javascript without the need for any external plugins like Flash, Java or Flex.</p>
<p>Many database features are supported, such as keys, foreign key constraints, comments and indexes. You can either save your design , print it or export as SQL script. It is possible to import schema from existing database. The application supports MySQL, Sqlite, PostgreSQL ,MSSql.</p>
<p>The application allows you to:<br />
1. Draw E-R designs<br />
2. Edit tables and rows<br />
3. Manage keys<br />
4. Create relations (FK constraints)<br />
5. Save &amp; Load designs<br />
6. Import DB schemas</p>
<p>The importing of MySQL databases can take some time, so make sure you set PHP timeout to more than 60 seconds to avoid errors. A sample WordPress 2.7 schema imported is shown below.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2009/02/image21.gif" target="_self"><img class="aligncenter size-full wp-image-264" style="border: 1px solid #c0c0c0;" title="image21" src="http://www.codediesel.com/wp-content/uploads/2009/02/image21.gif" alt="image21" width="884" height="457" /></a></p>
<p><strong>Installation:</strong><br />
Installation is simple. Just download the code from <a href="http://code.google.com/p/wwwsqldesigner/downloads/list">here</a>, change your database settings in the &#8216;sqldesigner\backend\php-mysql\index.php&#8217; (if you are using PHP &amp; MySQL) file and you are ready.</p>
<p>A live demo of the application can be found <a href="http://ondras.zarovi.cz/sql/demo/?keyword=default">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/mysql/browser-based-sql-schema-designer/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Benchmarking PHP code</title>
		<link>http://www.codediesel.com/php/benchmarking-php-code/</link>
		<comments>http://www.codediesel.com/php/benchmarking-php-code/#comments</comments>
		<pubDate>Tue, 24 Jun 2008 10:36:28 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=50</guid>
		<description><![CDATA[Code tuning is an essential part of programming. But most programmers would rather try to complete the project in hand then spend time optimizing a piece of code. A well tuned and optimized code block can speedup your application by an order of magnitude or even more. Many people confuse profiling with benchmarking. A profiler [...]]]></description>
			<content:encoded><![CDATA[<p>Code tuning is an essential part of programming. But most programmers would rather try to complete the project in hand then spend time optimizing a piece of code. A well tuned and optimized code block can speedup your application by an order of magnitude or even more.</p>
<p>Many people confuse profiling with benchmarking. A profiler is a analysis tool that lets you measure the performance of code at runtime. Profiling an application lets you find bottlenecks in your application code. You can find which part of your code is taking most of the time, you can than optimize that part of code. For example if there are 4 functions in  your application, profiling may report the following distribution:<br />
<span id="more-50"></span><br />
function a: 68%<br />
function b: 12%<br />
function c: 1.3%<br />
function d: 18.7%</p>
<p>i.e function &#8216;a&#8217; accounts for about 70% of the execution time in your application; if you can optimize that function it will boost your application performance considerably. If you had executed the above code on a laptop, and than executed the same on a fast server the code may run in half the time, but the distribution will remain the same. The distribution is what is important in profiling.</p>
<p>Benchmarking is different. It lets you find exactly how long it takes to execute a piece of code. For example I have developed two versions of a sorting algorithm for a application : Sort1(), and Sort2(). If I&#8217;ve to find which is faster than I can use a benchmarking software for the same.</p>
<p>To take a contrived example, I recently implemented a Linked List class in PHP (brushing up on my data structure skills), with all the usual functions a linked-list requires &#8211; &#8216;<em>deleteNode</em>&#8216;, &#8216;<em>insertFirstNode</em>&#8216;, &#8216;<em>insertLastNode</em>&#8216;, &#8216;<em>reverseList</em>&#8216; etc. I was quite happy with the outcome untill I ran a benchmark between two functions &#8211; &#8216;<em>insertFirstNode</em>&#8216; and &#8216;<em>insertLastNode</em>&#8216;. Here is a benchmark report for the two functions for various amount of nodes, in table and graph format. The last column shows by how much the &#8216;<em>insertLastNode</em>&#8216; is slower than the &#8216;<em>insertFirstNode</em>&#8216; function.</p>
<p><a href='http://www.codediesel.com/wp-content/uploads/2008/06/benchmark.gif'><img src="http://www.codediesel.com/wp-content/uploads/2008/06/benchmark.gif" alt="" title="benchmark" width="393" height="553" class="aligncenter size-full wp-image-51" /></a><br />
<a href='http://www.codediesel.com/wp-content/uploads/2008/06/benchmark2.gif'><img src="http://www.codediesel.com/wp-content/uploads/2008/06/benchmark2.gif" alt="benchmark2" title="benchmark2" width="460" height="348" class="aligncenter size-full wp-image-52" /></a></p>
<p>As you can see from the report &#8216;<em>insertLastNode</em>&#8216; takes exponential time to insert nodes. For example, when inserting 3000 nodes, &#8216;<em>insertLastNode</em>&#8216; is 30 times slower then &#8216;<em>insertFirstNode</em>&#8216;. The reason for this is that &#8216;<em>insertLastNode</em>&#8216; has to traverse the entire list to find the last node before adding a new node, while &#8216;<em>insertFirstNode</em>&#8216; always adds a new node at the beginning of the list. The simple solution as all you &#8216;data structure fans&#8217; know is to add a pointer that points to the last node of the list; so every time you have to add a node to the end of the list you use the end node pointer. After making the necessary changes I ran the benchmark and here are the results.</p>
<p><a href='http://www.codediesel.com/wp-content/uploads/2008/06/benchmark3.gif'><img src="http://www.codediesel.com/wp-content/uploads/2008/06/benchmark3.gif" alt="benchmark3" title="benchmark3" width="457" height="347" class="aligncenter size-full wp-image-53" /></a></p>
<p>As you can see now both the functions take an equal amount of time to insert a node. Before the code tuning &#8216;insertLastNode&#8217; took more than 6 seconds to insert 3000 nodes, while after tuning less than quarter of a second.</p>
<p>The whole point of this exercise is to tell you that code tuning can make a tremendous difference to how your application performs. </p>
<p><strong>Installation</strong><br />
You can download the Benchmark pacakage from <a href="http://pear.php.net/package/Benchmark">here</a> or use the pear installer like below:</p>

<div class="wp_codebox"><table><tr id="p5050"><td class="code" id="p50code50"><pre class="php" style="font-family:monospace;">c<span style="color: #339933;">:</span>\pear install Benchmark</pre></td></tr></table></div>

<p><strong>Using the package:</strong><br />
Using the package for benchmarking is simple enough. A implementation is shown below.</p>

<div class="wp_codebox"><table><tr id="p5051"><td class="code" id="p50code51"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* The following sample function is what we
       want to test */</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> someFunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1000</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000088;">$d</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sqrt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #009933; font-style: italic;">/* include the PEAR::Benchmark package */</span>
    <span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'Benchmark/Iterate.php'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$bench</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Benchmark_Iterate<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Run the above function 100 times.
       If you want to pass parameters to the function,
       you can add it after the function name like this:
       $bench-&gt;run(100, 'someFunction', para1, para2, para n);
    */</span>
    <span style="color: #000088;">$bench</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">run</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'someFunction'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Get the results */</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$bench</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Display the mean time the function 'someFunction'
       takes to execute */</span>
    <span style="color: #000000; font-weight: bold;">print</span> <span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'mean'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>Output:</strong></p>

<div class="wp_codebox"><table><tr id="p5052"><td class="code" id="p50code52"><pre class="php" style="font-family:monospace;"><span style="color:#800080;">0.022045</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/benchmarking-php-code/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

