<?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>Michiel van der Velde &#187; PHP</title>
	<atom:link href="http://www.michielvdvelde.nl/category/scripting-and-programming/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.michielvdvelde.nl</link>
	<description>A statistical impossibility</description>
	<lastBuildDate>Tue, 27 Apr 2010 16:01:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Namespaces in php 5.3</title>
		<link>http://www.michielvdvelde.nl/2010/01/namespaces-in-php-5-3/</link>
		<comments>http://www.michielvdvelde.nl/2010/01/namespaces-in-php-5-3/#comments</comments>
		<pubDate>Sat, 30 Jan 2010 12:47:53 +0000</pubDate>
		<dc:creator>Michiel</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[namespaces]]></category>
		<category><![CDATA[php 5.3]]></category>

		<guid isPermaLink="false">http://www.michielvdvelde.nl/?p=149</guid>
		<description><![CDATA[PHP 5.3, introduced in June 2009 and has some really interesting enhancements. Some of you may know about the promise of namespace support in php 6. Because this was a popular requested feature, namespaces have been introduced in php 5.3. What are namespaces and why are they convenient? &#8216;Namespaces&#8217; are default in almost every other [...]]]></description>
			<content:encoded><![CDATA[<p>PHP 5.3, introduced in June 2009 and has some really interesting enhancements. Some of you may know about the promise of namespace support in php 6. Because this was a popular requested feature, namespaces have been introduced in php 5.3.</p>
<h2>What are namespaces and why are they convenient?</h2>
<p>&#8216;Namespaces&#8217; are default in almost every other (OOP) language, such as C++ and C#. They separate classes and other objects (such as methods) in logical units, mostly to avoid name collisions. A program (or web site, as is probably the case in php) may use several frameworks and libraries. These may hold methods and/or classes that are named the same, for example a class named <em>Date</em>. Prior to php 5.3, this would lead to the well known error &#8220;<em>Could not redeclare class <strong>Date</strong> in file/name.php on line no</em>&#8220;. This, of course, could be a problem when you want to use multiple frameworks and/or third-party libraries.</p>
<p>The problem of name colissions was dealt with before by using something called &#8216;poor man&#8217;s namespacing&#8217;, which has been used in, for example, the Zend Framework. This looks something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #000000; font-weight: bold;">class</span> Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive <span style="color: #000000; font-weight: bold;">extends</span> Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum
<span style="color: #009900;">&#123;</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;">addFilter</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Zend_Search_Lucene_Analysis_TokenFilter_LowerCase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This of course is not very handy all the time. So, how do namespaces solve this problem?</p>
<h2>Namespaces in php 5.3</h2>
<p>A namespace in php can be thought of as an extra layer around (part of) your code. Every class en method name within it is unique and does not conflict with classes or methods with the same name in other namespaces.</p>
<p>To declare a namespace in php, use the keyword &#8216;namespace&#8217; on the first line of your file. Your classes and methods you define below it. All code within that file will be in that namespace. Let&#8217;s look at an example on how to use namespaces:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #000000; font-weight: bold;">namespace</span> MichielvdVelde\Core<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Database
<span style="color: #009900;">&#123;</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: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The class <em>Database</em> will now reside in the namespace <em>MichielvdVelde\Core</em>. If you were to make another namespace, which also holds a class named Database, this would be fine.</p>
<p>So, how do you use classes within a namespace? This is really kind of simple. There are two method.</p>
<h3>Method one</h3>
<p>Add the namespace to the declaration of the class. Like this:</p>
</pre>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'MichielvdVelde/Core.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MichielvdVelde\Core\Database<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>But this method is not really an improvement over poor man's namespacing. Therefore, there is a second method.</p>
<h3>Method two</h3>
<p>By using the 'use' keyward, you cam import namespaces in your code. This more closely resembles namespacing as implemented in other languages such as C++ and C#.</p>
</pre>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'MichielvdVelde/Core.php'</span><span style="color: #339933;">;</span>
&nbsp;
use MichielvdVelde\Core <span style="color: #b1b100;">as</span> CORE<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$db</span><span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CORE\Database<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As you can see, this is more aliasing than really importing. But still, this method is very useful.</p>
<h2>Gotcha's</h2>
<h3>Functions are also part of namespaces</h3>
<p>When defining functions in files with the 'namespace' keyword at the top, thsey are also part of that namespace.</p>
</pre>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #000000; font-weight: bold;">namespace</span> MichielvdVelde\Core<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> getDatabase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</pre>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'MichielvdVelde/Core.php'</span><span style="color: #339933;">;</span>
&nbsp;
getDatabase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Geeft E_FATAL error: Undefined function getDatabase()</span>
&nbsp;
use MichielvdVelde\Core <span style="color: #b1b100;">as</span> CORE<span style="color: #339933;">;</span>
&nbsp;
CORE\getDatabase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// This does work</span></pre></div></div>

<h3>Autoload changes</h3>
<p>Autoload on Windows does not use the \ very well. You may need to change your autoload function for this:</p>
</pre>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #000000; font-weight: bold;">function</span> __autoload<span style="color: #009900;">&#40;</span><span style="color: #000088;">$className</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$className</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'\\'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">DIRECTORY_SEPARATOR</span><span style="color: #339933;">,</span> <span style="color: #000088;">$className</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.php'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">require</span> <span style="color: #000088;">$className</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MichielveVelde\Core\Database<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Also note that you need to define __autoload within the global scope. If you define it within a namespace, php won't find it. If you do want to use the autolaod function from within a namespace, use the <em>spl_autoload_register</em> function:</p>
</pre>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">spl_autoload_register</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'MichielvdVelde\\Core\\Autoloader'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Conclusion</h2>
<p>PHP 5.3 introduces support for namespaces which will be very handy in organizing and cleaning up your code. Although it may take some time for web hosters to support php 5.3, you can experiment with it by installing the latest version of <a title="XAMPP" href="http://www.apachefriends.org/en/xampp.html" target="_blank">XAMPP</a> on your omputer, which has php 5.3 included.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.michielvdvelde.nl/2010/01/namespaces-in-php-5-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Security article: Password security in PHP</title>
		<link>http://www.michielvdvelde.nl/2009/12/security-article-password-security-in-php/</link>
		<comments>http://www.michielvdvelde.nl/2009/12/security-article-password-security-in-php/#comments</comments>
		<pubDate>Fri, 11 Dec 2009 13:59:08 +0000</pubDate>
		<dc:creator>Michiel</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.michielvdvelde.nl/?p=71</guid>
		<description><![CDATA[This article will deal with password security. Especially, passwords of your web site&#8217;s (or program&#8217;s) users. Although the title of this posts suggests it only applies to PHP scripts, it can in fact be used for and programming or scripting language (except the PHP examples, or course). On many web sites, visitors can register themselves, [...]]]></description>
			<content:encoded><![CDATA[<p>This article will deal with password security. Especially, passwords of your web site&#8217;s (or program&#8217;s) users. Although the title of this posts suggests it only applies to PHP scripts, it can in fact be used for and programming or scripting language (except the PHP examples, or course).</p>
<p>On many web sites, visitors can register themselves, for example to be able to post on the forums or place comments. These users have to fill in a password, that allows only them to log in with that specific name on that specific user account.<br />
But, how is this password stored? There are basically three methods, ranging from dumbest to smartest:</p>
<ol>
<li>Clear text, the password directly into the database or other storage medium;</li>
<li>Encrypted, the password encrypted with an algorithm (e.g. AES), with a key;</li>
<li>Hashed; a one-way hash (e.g. MD5, SHA1).</li>
</ol>
<p>As you might suspect, <strong>storing the password as clear text is the most idiotic thing you can do</strong>! Imagine a hacker breaks into your database; he instantly has all passwords for all users on your web site. Is that what you want? I think not.</p>
<p>Option two is storing the password as encrypted text. This requires an encryption algorithm, such as <a title="Advanced Encryption Standard" href="http://en.wikipedia.org/wiki/Advanced_Encryption_Standard" target="_blank">Advanced Encryption Standard</a>, and a key. This requires the key to be stored as well, and no matter how good you put it away. On the other hand, it allows you to decrypt the password and use it for verification. And you can give them their original password when they&#8217;ve lost it. But this still isn&#8217;t the best solution. See option three.</p>
<p><strong>Option three: hashes<br />
</strong>The third option is in my opinion, and that of a lot of people who know, hashing. A hash is, according to Wikipedia:</p>
<blockquote><p>A <strong>cryptographic hash function</strong> is a <a title="Algorithm" href="http://en.wikipedia.org/wiki/Algorithm">deterministic procedure</a> that takes an arbitrary block of <a title="Data" href="http://en.wikipedia.org/wiki/Data">data</a> and returns a fixed-size <a title="Bit" href="http://en.wikipedia.org/wiki/Bit">bit</a> string, the (<strong>cryptographic</strong>) <strong>hash value</strong>, such that an accidental or intentional change to the data will change the hash value. The data to be encoded is often called the &#8220;message&#8221;, and the hash value is sometimes called the <strong>message digest</strong> or simply <strong>digest</strong>.</p></blockquote>
<p>So, a hash is a string that is based 0n the original text. This is handy, as it is almost impossible to reverse the hash, so the password is safe. And when you need to check a password, you simply hash the inputted password too and compare.</p>
<p>What kind of hash functions are there? Basicly, the following two are the most used:</p>
<ol>
<li>MD5</li>
<li>SHA(1)</li>
</ol>
<p><strong>MD5<br />
</strong>MD5 stands for <strong>Message Digest 5</strong>, and has been developed by Ron Rivest in 1991 to replace MD4. How can you use it in php?</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Method one</span>
<span style="color: #000088;">$hash</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Method two</span>
<span style="color: #000088;">$hash</span> <span style="color: #339933;">=</span> <span style="color: #990000;">hash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'MD5'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This results in a 32-digits hexadecimal string, for example <strong>5f4dcc3b5aa765d61d8327deb882cf99</strong>. This is always the same for the same string. This provides a great method for password saving, because the password can never (or, with extreme difficulty) be reverse-engineered. When you need to check a password, you simply hash that too, and compare the strings.</p>
<p><strong>SHA1<br />
</strong>SHA1 is another cryptographic hash function. According to Wikipedia:</p>
<blockquote><p>The <strong>SHA hash functions</strong> are a set of <a title="Cryptographic hash function" href="http://en.wikipedia.org/wiki/Cryptographic_hash_function">cryptographic hash functions</a> designed by the <a title="National Security Agency" href="http://en.wikipedia.org/wiki/National_Security_Agency">National Security Agency</a> (NSA) and published by the <a title="National Institute of Standards and Technology" href="http://en.wikipedia.org/wiki/National_Institute_of_Standards_and_Technology">NIST</a> as a U.S. <a title="Federal Information Processing Standard" href="http://en.wikipedia.org/wiki/Federal_Information_Processing_Standard">Federal Information Processing Standard</a>. SHA stands for <strong>Secure Hash Algorithm</strong>.</p></blockquote>
<p>SHA1 generates a hexadecimal string of 40 characters, instead of the 32 of MD5. SHA1 is considered more secure. Using this in php is not more difficult:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Method one</span>
<span style="color: #000088;">$hash</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sha1</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Method two</span>
<span style="color: #000088;">$hash</span> <span style="color: #339933;">=</span> <span style="color: #990000;">hash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SHA1'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This generates the has, for example<strong> 5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8</strong>.</p>
<p>This is it for now. I&#8217;ll write an article about cracking hash ciphers in the furure, which is mainly <strong>brute-forcing</strong>. Bye-bye.</p>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 339px; width: 1px; height: 1px;">A <strong>cryptographic hash function</strong> is a <a title="Algorithm" href="http://en.wikipedia.org/wiki/Algorithm">deterministic procedure</a> that takes an arbitrary block of <a title="Data" href="http://en.wikipedia.org/wiki/Data">data</a> and returns a fixed-size <a title="Bit" href="http://en.wikipedia.org/wiki/Bit">bit</a> string, the (<strong>cryptographic</strong>) <strong>hash value</strong>, such that an accidental or intentional change to the data will change the hash value. The data to be encoded is often called the &#8220;message&#8221;, and the hash value is sometimes called the <strong>message digest</strong> or simply <strong>digest</strong>.</div>
]]></content:encoded>
			<wfw:commentRss>http://www.michielvdvelde.nl/2009/12/security-article-password-security-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How-To: Use the PHP Data Objects class to access your database</title>
		<link>http://www.michielvdvelde.nl/2009/12/how-to-use-the-php-data-objects-class-to-access-your-database/</link>
		<comments>http://www.michielvdvelde.nl/2009/12/how-to-use-the-php-data-objects-class-to-access-your-database/#comments</comments>
		<pubDate>Sat, 05 Dec 2009 14:48:28 +0000</pubDate>
		<dc:creator>Michiel</dc:creator>
				<category><![CDATA[Guides]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.michielvdvelde.nl/?p=64</guid>
		<description><![CDATA[If you&#8217;re a PHP developer, you are of course aware of PDO; PHP Data Objects. Or are you? PDO is the new form to communicate with databases, for example MySQL, PostgreSQL and MSSQL. When you&#8217;re using MySQL, you may be using the following methode to connect to your database: $connection = new mysqli&#40;'localhost','username','password'&#41;; Or, even [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re a PHP developer, you are of course aware of PDO; PHP Data Objects. Or are you?<br />
PDO is the new form to communicate with databases, for example MySQL, PostgreSQL and MSSQL. When you&#8217;re using MySQL, you may be using the following methode to connect to your database:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$connection</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> mysqli<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Or, even worse:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This of course is hopelessly outdated, and with PHP version 6 on the way, will soon be removed from the standard installed libraries. But there is a better, more Object Oriented way to connect to your database, and that is PHP Data Objects.</p>
<p><strong>What is PDO?<br />
</strong>According to <a title="php.net" href="http://www.php.net/manual/en/intro.pdo.php" target="_blank">php.net</a>:</p>
<blockquote><p>The <em>PHP Data Objects</em> (<acronym>PDO</acronym>) extension defines a lightweight, consistent interface    for accessing databases in PHP. Each database driver that    implements the PDO interface can expose database-specific    features as regular extension functions. Note that you cannot    perform any database functions using the PDO extension by    itself; you must use a <a href="http://www.php.net/manual/en/pdo.drivers.php" target="_blank">database-specific    PDO driver</a> to access a database server.</p>
<p>PDO provides a <em>data-access</em> abstraction layer, which    means that, regardless of which database you&#8217;re using, you use the same    functions to issue queries and fetch data.  PDO does    <em>not</em> provide a <em>database</em> abstraction; it doesn&#8217;t rewrite SQL or emulate missing features.  You    should use a full-blown abstraction layer if you need that facility.</p>
<p>PDO ships with PHP 5.1, and is available as a PECL extension for PHP 5.0;    PDO requires the new OO features in the core of PHP 5, and so will not    run with earlier versions of PHP.</p></blockquote>
<p><strong>Connecting to your MySQL database</strong><br />
So, how does one use it? Below is an example for MySQL (other databases might require a slightly different approach):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$connectionString</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;mysql:host=localhost;dbname=database&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$pdo</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PDO<span style="color: #009900;">&#40;</span><span style="color: #000088;">$connectionString</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now you can use the <em>$pdo</em> variable to do things, e.g.:</p>
<p><strong>Retrieve information from tables</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pdo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM table&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$results</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'field'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is how you can read data from your database tables. Note that you don&#8217;t use the <em>while</em> loop and the <em>fetch_num</em> or <em>fetch_assoc </em>(or similar) in this case, but a <em>foreach </em>loop. You can access the field values as you would in an ordinary array.</p>
<p><strong>Queries that don&#8217;t return anything: the wrong way to do it<br />
</strong>For queries that don&#8217;t return anything, for example <em>INSERT</em> and <em>UPDATE</em> queries, PDO provides the <em>exec</em> method. This method returns the amount of rows affected (if any) by the query. Using it is simple:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$pdo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">exec</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT INTO 'table' (id, value) VALUES ('1','this is the value')&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>But this method is susceptible to <a title="SQL injection on Wikipedia" href="http://en.wikipedia.org/wiki/SQL_injection" target="_blank">SQL injections</a>. Therefore, the PDO class gives us another method to insert or alter information in/from the database: <strong>prepared statements<em>. </em></strong>A prepared statement is SQL injection safe and the right way to do things, especially if you need to insert or alter user submitted information. Here an example of how to use prepared statements:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$stmt</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pdo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT INTO table (name, value) VALUES (:name, :value)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':name'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':value'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// insert a row</span>
<span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'one'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is a safe way to insert or update data in your database. Of course you can use prepared statements with SELECT queries as well.</p>
<p><strong>The Basics<br />
</strong>This provides you with the basics to select, insert and update data from.to your MySQL database. Later I will expand on this subject, and dive into the more complex possibilities of PDO, as wel as how to access other databases than MySQL.</p>
<p><strong>Related Links<br />
</strong></p>
<ul>
<li><a title="PDO op php.net" href="http://php.net/manual/en/book.pdo.php" target="_blank">PDO on php.net</a></li>
<li><a title="SQL injection on Wikipedia" href="http://en.wikipedia.org/wiki/SQL_injection" target="_blank">SQL Injection on Wikipedia</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.michielvdvelde.nl/2009/12/how-to-use-the-php-data-objects-class-to-access-your-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
