<?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 With Design &#187; php</title>
	<atom:link href="http://codewithdesign.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://codewithdesign.com</link>
	<description>web application development blog by Caleb Jonasson</description>
	<lastBuildDate>Fri, 11 May 2012 12:28:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>URL Authentication &#8211; A New Approach</title>
		<link>http://codewithdesign.com/2011/06/21/url-authentication-a-new-approach/</link>
		<comments>http://codewithdesign.com/2011/06/21/url-authentication-a-new-approach/#comments</comments>
		<pubDate>Tue, 21 Jun 2011 14:58:27 +0000</pubDate>
		<dc:creator>Caleb Jonasson</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[url]]></category>
		<category><![CDATA[url changes]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://codewithdesign.com/?p=557</guid>
		<description><![CDATA[TweetIt is time for web developers and software engineers to make a new approach when checking the validity of URLs and emails that are provided by users. Icann has decided that it is going to allow new suffix&#8217; and the ability to host a website on a domain that has no suffix ie: &#8216;http://codewithdesign/index.php&#8217;. With [...]
Related posts:<ol>
<li><a href='http://codewithdesign.com/2010/09/24/check-email-function-php/' rel='bookmark' title='Check Email Function PHP'>Check Email Function PHP</a></li>
<li><a href='http://codewithdesign.com/2011/07/02/password-recovery-theory/' rel='bookmark' title='Password Recovery {Theory}'>Password Recovery {Theory}</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div style = 'float:left; margin-right:5px;'>            <a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" data-text="URL Authentication &#8211; A New Approach" data-via="" data-url="http://codewithdesign.com/2011/06/21/url-authentication-a-new-approach/" en>Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div class="plus-one-wrap"><g:plusone size="tall" href="http://codewithdesign.com/2011/06/21/url-authentication-a-new-approach/"></g:plusone></div><p>It is time for web developers and software engineers to make a new approach when checking the validity of URLs and emails that are provided by users. Icann has decided that it is going to allow new suffix&#8217; and the ability to host a website on a domain that has no suffix ie: &#8216;http://codewithdesign/index.php&#8217;. With this change in URLs means that all regular expressions are going to have to not force a suffix or that last decimal as a root URL.</p>
<h2>How Will The New Domains Affect Me?</h2>
<p>The new domain scheme is going to change how your website validates proper emails and URLs which can be a rather large change on websites that do not have formatting called from one place. Because of this change you will have to overhaul your website/blog/app to support new URLs and emails.</p>
<h2>How Should I Go About Making The Change</h2>
<p>If you haven&#8217;t done so already it is a good idea to make sure that your formatting and checking is coming from one place and will be able to handle the errors accordingly. The first thing that is required is either a functions file or a class file that will support multiple formats of input. This way when something like this changes you only need to update the code once.</p>
<h2>Ways To Check URLs And Emails</h2>
<p>When working with a URL you can change your regular expression to just check for proper characters and the presence of &#8216;http://&#8217; or &#8216;https://&#8217;, but there is a much more fun way to check for the new format as well. You should still be using a filter or a regular expression but make sure that your version of PHP is high enough if you are going to use a filter.</p>
<h2>Check For An Existing Email</h2>
<p>Because of the new URL scheme we are going to need to handle the case that the user is from a website such as &#8216;http://bearattacksarenotgood/&#8217; This will require us to check for an existing email without the checking of a proper suffix in the email since a user can have the email &#8216;calebjonasson@bearattacksarenotgood&#8217;.</p>
<p>We can easily check for an existing email by using a function called &#8216;checkdnsrr&#8217; but first we need to split the email name from the URL which will give us &#8216;calebjonasson&#8217; and &#8216;bearattacksarenotgood&#8217; which we can accomplish by using the list function will will break apart a string by finding a character.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//load a variable with an email.</span>
<span style="color: #000088;">$emailAddress</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'calebjonasson@bearattacksarenotgood'</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//split the email by the user name and domain name</span>
<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$userName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$domainName</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'@'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$emailAddress</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//use checkdnsrr to validate the domain names existence.</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">checkdnsrr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$domainName</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'MX'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//return value on success.</span>
    <span style="color: #b1b100;">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: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//return value on failure.</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h2>Pinging A Server</h2>
<p>It is possible to ping a server through PHP and it is also possible to just use a cURL to request the page information and check and the response headers. The first thing I will show you is how to send a request which will allow you to check for ping of the data server in question.</p>
<p>My prefered method of doing so is to install net ping onto the server. This is the best solution that I have found on the web to this day and is very simple to use. Here is a tutorial from <a title="Code Diesel" href="http://www.codediesel.com/php/ping-a-server-using-php/" target="_blank">Code Diesel</a>.</p>
<img src="http://codewithdesign.com/?ak_action=api_record_view&id=557&type=feed" alt="" /><p>Related posts:<ol>
<li><a href='http://codewithdesign.com/2010/09/24/check-email-function-php/' rel='bookmark' title='Check Email Function PHP'>Check Email Function PHP</a></li>
<li><a href='http://codewithdesign.com/2011/07/02/password-recovery-theory/' rel='bookmark' title='Password Recovery {Theory}'>Password Recovery {Theory}</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://codewithdesign.com/2011/06/21/url-authentication-a-new-approach/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Redundant Data Class in PHP {Theory}</title>
		<link>http://codewithdesign.com/2011/05/30/redundant-data-class-in-php-theory/</link>
		<comments>http://codewithdesign.com/2011/05/30/redundant-data-class-in-php-theory/#comments</comments>
		<pubDate>Tue, 31 May 2011 06:51:50 +0000</pubDate>
		<dc:creator>Caleb Jonasson</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[data class]]></category>

		<guid isPermaLink="false">http://codewithdesign.com/?p=548</guid>
		<description><![CDATA[TweetAs a server side programmer you are going to be spending a lot of time working with data that is pulled from the database and in order to make sure that you do not have any loose ends it is a good idea to handle this situation with a class. This article is not going [...]
Related posts:<ol>
<li><a href='http://codewithdesign.com/2011/07/02/password-recovery-theory/' rel='bookmark' title='Password Recovery {Theory}'>Password Recovery {Theory}</a></li>
<li><a href='http://codewithdesign.com/2011/08/04/statistics-simple-logging-design/' rel='bookmark' title='Statistics &#8211; Simple Logging Design'>Statistics &#8211; Simple Logging Design</a></li>
<li><a href='http://codewithdesign.com/2010/09/22/sql-one-to-many-using-inner-join/' rel='bookmark' title='SQL: One To Many Using Inner Join'>SQL: One To Many Using Inner Join</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div style = 'float:left; margin-right:5px;'>            <a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" data-text="Redundant Data Class in PHP {Theory}" data-via="" data-url="http://codewithdesign.com/2011/05/30/redundant-data-class-in-php-theory/" en>Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div class="plus-one-wrap"><g:plusone size="tall" href="http://codewithdesign.com/2011/05/30/redundant-data-class-in-php-theory/"></g:plusone></div><p>As a server side programmer you are going to be spending a lot of time working with data that is pulled from the database and in order to make sure that you do not have any loose ends it is a good idea to handle this situation with a class. This article is not going to cover how to code a data class nor is it going to help you create a specific data class. This article is here to show you how to make your data redundant and error proof.</p>
<h2>Pulling all of your information</h2>
<p>When pulling the information off the get go you are going to want to do one thing and one thing only: query all of the data that you can use in the class. The query is going to take place inside of a function and will most likely be used every time that you are in need of the data in the database. Once the query to obtain this information has taken place then you are going to either have results or no results and the easy way to check is simply by checking the number of rows returned.</p>
<p>Say we were trying to access user information for a user page. It would be a good idea to query and make sure that all of the information is available. In this query we are most likely going to be joining up to multiple tables because having all of the user information stored in one table would create a fair amount of overhead and would increase the time it took to go through the table. For this reason we break things apart.</p>
<h2>An Example Of Database Tables</h2>
<p>A well structured database table is going to be pulling from an auto incrementing ID. This auto incrementing id is going to be found on every table but since we can not just assume that if we insert a record into tbl_user and tbl_user_profile at the same time we are going to get ID&#8217;s that match properly. Because this is a flawed way of looking at the table set up we are going to use the auto incremental ID from the tbl_user and plug this into a new column on the other user tables. This way we can simply join the tbl_user.user_id to tbl_user_profile.user_nid. Now that we have a relationship in the database that will work and is pretty redundant in theory we are still going to have to deal with the situation of a table not being created properly upon user registration and thus we are lead to&#8230;</p>
<h2>Enforcing Existing Tables</h2>
<p>Remember back up at the top when I was talking about the query all function either working or not working? Well this function upon returning zero rows will tell us one of two things. (Assuming the SQL was written properly.) The first thing that it will tell us is that this user does not exist, and the second thing is that the user is missing a table. This could mean that something was deleted, or maybe the table was recovered in backup at a later date and there was a record missing. Either way we know that by checking the user table for the users existence we are able to confirm that we are dealing with missing data and this is where the checking functions come into place.</p>
<p>In a well written data handling class there will be functions that exist to pull results from each of the individual tables. These functions are excellent for checking that a user does exist in the following table and are a great way to quickly and easily pull sections of content based on the user which are handy when loading content on an interval via AJAX. But now we are getting a little off topic. Creating a function that can pull information and return a number as a status and creating another function that will insert defaults into the table is an excellent way to make sure that you do not lose data and that all of the data is being pulled properly.</p>
<h2>Recap</h2>
<p>When using the class you are most likely just going to need to get the information which means that you will be using a function that behaves like the queryAll that I was talking about. If the user does not exist then you can simply return false.</p>
<p>The next step is to create functions that will check individual tables based on that original tbl_user.user_id (or whatever yours is) starting with the initial tbl_user which the rest of the tables are based on. This will tell you if it was created in the first place or not.</p>
<p>If we have gotten far enough to know that the user does exist and the user simply doesn&#8217;t have a record in one of the tables then we can query each of the tables and find out where the record is missing. Now that we know the table we can simply insert blank data into the table and maybe send a notification to the user that they may need to update a certain part of their profile.</p>
<h2>Knowing when we have an error in table creation</h2>
<p>Through this data class it is just a matter of adding in an error log message to let the administrators tell if the application has a bug in it but this shouldn&#8217;t be a problem if you are handling proper inserts and updates through SQL and checking for an affected row upon creation.</p>
<img src="http://codewithdesign.com/?ak_action=api_record_view&id=548&type=feed" alt="" /><p>Related posts:<ol>
<li><a href='http://codewithdesign.com/2011/07/02/password-recovery-theory/' rel='bookmark' title='Password Recovery {Theory}'>Password Recovery {Theory}</a></li>
<li><a href='http://codewithdesign.com/2011/08/04/statistics-simple-logging-design/' rel='bookmark' title='Statistics &#8211; Simple Logging Design'>Statistics &#8211; Simple Logging Design</a></li>
<li><a href='http://codewithdesign.com/2010/09/22/sql-one-to-many-using-inner-join/' rel='bookmark' title='SQL: One To Many Using Inner Join'>SQL: One To Many Using Inner Join</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://codewithdesign.com/2011/05/30/redundant-data-class-in-php-theory/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>cURL 403 Error Returning</title>
		<link>http://codewithdesign.com/2011/05/26/curl-403-error-returning/</link>
		<comments>http://codewithdesign.com/2011/05/26/curl-403-error-returning/#comments</comments>
		<pubDate>Thu, 26 May 2011 13:40:12 +0000</pubDate>
		<dc:creator>Caleb Jonasson</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[cURL]]></category>
		<category><![CDATA[cURL returns 403]]></category>
		<category><![CDATA[http status 403 return]]></category>

		<guid isPermaLink="false">http://codewithdesign.com/?p=539</guid>
		<description><![CDATA[TweetThe other day at work we ran into an issue where the server would return a 403 error page when retrieving page information from a cURL call. After searching around the web for a while thinking that we had a server permission issue on our hands it ended up just being a PHP problem. In [...]
Related posts:<ol>
<li><a href='http://codewithdesign.com/2011/06/21/url-authentication-a-new-approach/' rel='bookmark' title='URL Authentication &#8211; A New Approach'>URL Authentication &#8211; A New Approach</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div style = 'float:left; margin-right:5px;'>            <a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" data-text="cURL 403 Error Returning" data-via="" data-url="http://codewithdesign.com/2011/05/26/curl-403-error-returning/" en>Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div class="plus-one-wrap"><g:plusone size="tall" href="http://codewithdesign.com/2011/05/26/curl-403-error-returning/"></g:plusone></div><p>The other day at work we ran into an issue where the server would return a 403 error page when retrieving page information from a cURL call. After searching around the web for a while thinking that we had a server permission issue on our hands it ended up just being a PHP problem.</p>
<p>In order to make a cURL request from your own server you must first <strong>make sure that the session has been destroyed prior to and cURL commands.</strong> This is because your server cannot have two pages that can access sessions up at the same time and the primary file that you are working from is going to lock the secondary file that you are trying to bring in.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//authentication code.</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//destroy session first.</span>
<span style="color: #990000;">session_destroy</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//cURL code.</span>
<span style="color: #000088;">$ch_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POST<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POSTFIELDS<span style="color: #339933;">,</span> <span style="color: #000088;">$fields_string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span>
<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//now we can start the session again</span>
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//do some other stuff...</span></pre></td></tr></table></div>

<p>My only guess as to why PHP does this is to protect itself from breaking sessions with multiple page access which is a pretty good security issue to have in place. Better error reporting would have been nice though.</p>
<img src="http://codewithdesign.com/?ak_action=api_record_view&id=539&type=feed" alt="" /><p>Related posts:<ol>
<li><a href='http://codewithdesign.com/2011/06/21/url-authentication-a-new-approach/' rel='bookmark' title='URL Authentication &#8211; A New Approach'>URL Authentication &#8211; A New Approach</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://codewithdesign.com/2011/05/26/curl-403-error-returning/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting Secured Using Mysqli</title>
		<link>http://codewithdesign.com/2011/04/09/getting-secured-using-mysqli/</link>
		<comments>http://codewithdesign.com/2011/04/09/getting-secured-using-mysqli/#comments</comments>
		<pubDate>Sat, 09 Apr 2011 12:32:11 +0000</pubDate>
		<dc:creator>Caleb Jonasson</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[mysqli]]></category>
		<category><![CDATA[prepare]]></category>
		<category><![CDATA[stmt]]></category>

		<guid isPermaLink="false">http://codewithdesign.com/?p=494</guid>
		<description><![CDATA[TweetIntroduction The Mysqli class is a great help to working towards proper security when sending and pulling information from the database. It can stop injection attacks in their tracks and will allow you to specify data types allowing you to not let anything bad slip into the system. In this article I will be going [...]
Related posts:<ol>
<li><a href='http://codewithdesign.com/2011/04/08/searching-with-in-a-prepared-statement/' rel='bookmark' title='Searching With %?% In a Prepared Statement'>Searching With %?% In a Prepared Statement</a></li>
<li><a href='http://codewithdesign.com/2011/04/07/create-your-own-phpamf-data-service-for-flex/' rel='bookmark' title='Create Your Own PHP/AMF Data Service For Flex'>Create Your Own PHP/AMF Data Service For Flex</a></li>
<li><a href='http://codewithdesign.com/2010/09/22/sql-one-to-many-using-inner-join/' rel='bookmark' title='SQL: One To Many Using Inner Join'>SQL: One To Many Using Inner Join</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div style = 'float:left; margin-right:5px;'>            <a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" data-text="Getting Secured Using Mysqli" data-via="" data-url="http://codewithdesign.com/2011/04/09/getting-secured-using-mysqli/" en>Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div class="plus-one-wrap"><g:plusone size="tall" href="http://codewithdesign.com/2011/04/09/getting-secured-using-mysqli/"></g:plusone></div><h2>Introduction</h2>
<p>The Mysqli class is a great help to working towards proper security when sending and pulling information from the database. It can stop injection attacks in their tracks and will allow you to specify data types allowing you to not let anything bad slip into the system.</p>
<p>In this article I will be going through the step by step approach to creating a query using prepared statements through MySQL. We are going to follow best practices and use the object oriented style to achieve a connection and gather our results.</p>
<h2>Connecting to the database</h2>
<p>The first thing that we will be doing is connecting to the database. Traditionally this should be done using an outside class to prevent an access to the connection information. The connection string will attempt to connect to the database and open up a persistent connection.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">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: #339933;">,</span><span style="color: #0000ff;">'database'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>Creating the prepared statement</h2>
<p>The next thing that is required is for us to store the prepared statement. This should be attempted inside of an if statement. This way we can handle any errors appropriately.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT id, name, year FROM tbl_books WHERE name = ? AND year = ? LIMIT ?&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//store the prepared statement into the $stmt variable.</span>
<span style="color: #666666; font-style: italic;">//note that the $stmt is a common naming for prepared statements.</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connection</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">trigger_error</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connection</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">error</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>Wild cards</h2>
<p>You probably noticed that the sql code has something new inside of it. This is the wild card for the sql statement and a &#8216;?&#8217; is used to display it. This will allow us to bind variables in place of these questions marks and specify their data type.</p>
<h2>Bind parameters</h2>
<p>Bind parameters is the next step that takes place when there are wild cards inside of the sql statement. These allow us to force a data type into the format of our choosing. There are 4 data types that we can use.</p>
<p>&#8216;I&#8217; Integer<br />
&#8216;s&#8217; String<br />
&#8216;d&#8217; Decimal<br />
&#8216;b&#8217; Blob</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT id, name, year FROM tbl_books WHERE name = ? AND year = ? LIMIT ?&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//store the prepared statement into the $stmt variable.</span>
<span style="color: #666666; font-style: italic;">//note that the $stmt is a common naming for prepared statements.</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connection</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">//Now bind the data to the types.</span>
	<span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bind_param</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sii'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bookName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bookYear</span><span style="color: #339933;">,</span> <span style="color: #000088;">$limit</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">trigger_error</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connection</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">error</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The above function that we just used will take a minimum of two arguments, the first to define the variable types and everything after that is a variable. Notice that all of the type declarations are in the same string. A common error for this piece of code is that you have an invalid amount of arguments being passed. Another error commonly given is that you cannot have data placed directly in the string. You need to reference variables.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//This is not allowed:</span>
<span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bind_param</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sii'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;the php book&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>Execution</h2>
<p>Once we have properly set up our bindings we can execute the prepared statement and try to get a result from the database. As you can see in the code below; this can fail and an error message will be stored in case it does. This is why we are going to have it placed inside of an if statement.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT id, name, year FROM tbl_books WHERE name = ? AND year = ? LIMIT ?&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//store the prepared statement into the $stmt variable.</span>
<span style="color: #666666; font-style: italic;">//note that the $stmt is a common naming for prepared statements.</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connection</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">//Now bind the data to the types.</span>
	<span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bind_param</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sii'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bookName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bookYear</span><span style="color: #339933;">,</span> <span style="color: #000088;">$limit</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">//We will now execute the statement. This will throw an error if unsuccessful.</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</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: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">trigger_error</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">error</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><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">trigger_error</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connection</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">error</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>Number of Rows</h2>
<p>The next step in our quest for world domination is to store the results inside of the statement variable. This is going to let us make a call for the number of rows so we can handle the case of no rows being returned.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT id, name, year FROM tbl_books WHERE name = ? AND year = ? LIMIT ?&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//store the prepared statement into the $stmt variable.</span>
<span style="color: #666666; font-style: italic;">//note that the $stmt is a common naming for prepared statements.</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connection</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">//Now bind the data to the types.</span>
	<span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bind_param</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sii'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bookName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bookYear</span><span style="color: #339933;">,</span> <span style="color: #000088;">$limit</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">//We will now execute the statement. This will throw an error if unsuccessful.</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</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: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//check the number of rows.</span>
		<span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">store_result</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">num_rows</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Nothing was returned from the database.'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">trigger_error</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">error</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><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">trigger_error</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connection</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">error</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: #339933;">&lt;</span>pre<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>h2<span style="color: #339933;">&gt;</span>Binding Results to Variables<span style="color: #339933;">&lt;/</span>h2<span style="color: #339933;">&gt;</span>
The last thing that is required is binding the results and looping through the results<span style="color: #339933;">.</span> <span style="color: #b1b100;">As</span> you can see we are actually creating variables with this <span style="color: #000000; font-weight: bold;">function</span> and storing the information into them<span style="color: #339933;">.</span> The variables created are gathered from the select part of the statement<span style="color: #339933;">.</span> This means that we need to <span style="color: #990000;">define</span> the variables in the same order <span style="color: #b1b100;">as</span> the sql statement<span style="color: #339933;">.</span>
<span style="color: #339933;">&lt;</span>pre lang<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;PHP&quot;</span> line<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT id, name, year FROM tbl_books WHERE name = ? AND year = ? LIMIT ?&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//store the prepared statement into the $stmt variable.</span>
<span style="color: #666666; font-style: italic;">//note that the $stmt is a common naming for prepared statements.</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connection</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">//Now bind the data to the types.</span>
	<span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bind_param</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sii'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bookName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bookYear</span><span style="color: #339933;">,</span> <span style="color: #000088;">$limit</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">//We will now execute the statement. This will throw an error if unsuccessful.</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</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: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//check the number of rows.</span>
		<span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">store_result</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">num_rows</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//bind the results</span>
			<span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bind_result</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #339933;">,</span> <span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$year</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">//loop through the results.</span>
			<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetch</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: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$name</span>: <span style="color: #006699; font-weight: bold;">$year</span>&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Nothing was returned from the database.'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">trigger_error</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">error</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><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">trigger_error</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connection</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">error</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>It is now time to run your code and gather the results using the fetch function. If you only have to return one row from the database then you can just use the fetch function outside of the while loop and it will allow you to pull the results and use the variables in the same way you would in the while loop.</p>
<img src="http://codewithdesign.com/?ak_action=api_record_view&id=494&type=feed" alt="" /><p>Related posts:<ol>
<li><a href='http://codewithdesign.com/2011/04/08/searching-with-in-a-prepared-statement/' rel='bookmark' title='Searching With %?% In a Prepared Statement'>Searching With %?% In a Prepared Statement</a></li>
<li><a href='http://codewithdesign.com/2011/04/07/create-your-own-phpamf-data-service-for-flex/' rel='bookmark' title='Create Your Own PHP/AMF Data Service For Flex'>Create Your Own PHP/AMF Data Service For Flex</a></li>
<li><a href='http://codewithdesign.com/2010/09/22/sql-one-to-many-using-inner-join/' rel='bookmark' title='SQL: One To Many Using Inner Join'>SQL: One To Many Using Inner Join</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://codewithdesign.com/2011/04/09/getting-secured-using-mysqli/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Searching With %?% In a Prepared Statement</title>
		<link>http://codewithdesign.com/2011/04/08/searching-with-in-a-prepared-statement/</link>
		<comments>http://codewithdesign.com/2011/04/08/searching-with-in-a-prepared-statement/#comments</comments>
		<pubDate>Fri, 08 Apr 2011 10:17:43 +0000</pubDate>
		<dc:creator>Caleb Jonasson</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[%?%]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[wildcard]]></category>

		<guid isPermaLink="false">http://codewithdesign.com/?p=491</guid>
		<description><![CDATA[TweetA while back when working on a project I learned that when using the wild card: &#8216;%&#8217; in SQL with a prepared statement you are going to get an error message. This is because the prepared statement class has an issue with a wild card surrounding wild cards. &#8216;%?%&#8217;. Because of this a simple sql [...]
Related posts:<ol>
<li><a href='http://codewithdesign.com/2011/04/09/getting-secured-using-mysqli/' rel='bookmark' title='Getting Secured Using Mysqli'>Getting Secured Using Mysqli</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div style = 'float:left; margin-right:5px;'>            <a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" data-text="Searching With %?% In a Prepared Statement" data-via="" data-url="http://codewithdesign.com/2011/04/08/searching-with-in-a-prepared-statement/" en>Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div class="plus-one-wrap"><g:plusone size="tall" href="http://codewithdesign.com/2011/04/08/searching-with-in-a-prepared-statement/"></g:plusone></div><p>A while back when working on a project I learned that when using the wild card: &#8216;%&#8217; in SQL with a prepared statement you are going to get an error message. This is because the prepared statement class has an issue with a wild card surrounding wild cards. &#8216;%?%&#8217;.</p>
<p>Because of this a simple sql query such as this is going to have issues pulling data from the database.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM tbl_comments WHERE comment LIKE %?% LIMIT 20&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Rather than attempt to get the above code to work you are going to need to take a separate approach and store the wild cards in with the search variable. This only takes one extra line of code and your search will be back up and running in no time at all.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$comment</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'%'</span><span style="color: #339933;">.</span><span style="color: #000088;">$comment</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'%'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM tbl_comments WHERE comment LIKE ? LIMIT 20&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<img src="http://codewithdesign.com/?ak_action=api_record_view&id=491&type=feed" alt="" /><p>Related posts:<ol>
<li><a href='http://codewithdesign.com/2011/04/09/getting-secured-using-mysqli/' rel='bookmark' title='Getting Secured Using Mysqli'>Getting Secured Using Mysqli</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://codewithdesign.com/2011/04/08/searching-with-in-a-prepared-statement/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Stop Direct Page Access With PHP</title>
		<link>http://codewithdesign.com/2010/12/28/stop-direct-page-access-with-php/</link>
		<comments>http://codewithdesign.com/2010/12/28/stop-direct-page-access-with-php/#comments</comments>
		<pubDate>Wed, 29 Dec 2010 05:36:41 +0000</pubDate>
		<dc:creator>Caleb Jonasson</dc:creator>
				<category><![CDATA[htaccess]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[.htaccess]]></category>
		<category><![CDATA[access]]></category>
		<category><![CDATA[directory]]></category>
		<category><![CDATA[stop]]></category>

		<guid isPermaLink="false">http://codewithdesign.com/?p=463</guid>
		<description><![CDATA[TweetThere are a few ways to stop direct access of a page. I will go over a few simple techniques that will help out those of you that are just getting into PHP. Method number 1 Assuming that all of your pages require a function that was made by yourself and is brought in from [...]
Related posts:<ol>
<li><a href='http://codewithdesign.com/2010/09/24/creating-a-multi-page-site-from-one-page-using-get/' rel='bookmark' title='Creating a Multi-Page Site From One Page Using GET'>Creating a Multi-Page Site From One Page Using GET</a></li>
<li><a href='http://codewithdesign.com/2009/09/29/the-beginners-guide-to-php/' rel='bookmark' title='The Beginners Guide to PHP'>The Beginners Guide to PHP</a></li>
<li><a href='http://codewithdesign.com/2011/05/30/redundant-data-class-in-php-theory/' rel='bookmark' title='Redundant Data Class in PHP {Theory}'>Redundant Data Class in PHP {Theory}</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div style = 'float:left; margin-right:5px;'>            <a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" data-text="Stop Direct Page Access With PHP" data-via="" data-url="http://codewithdesign.com/2010/12/28/stop-direct-page-access-with-php/" en>Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div class="plus-one-wrap"><g:plusone size="tall" href="http://codewithdesign.com/2010/12/28/stop-direct-page-access-with-php/"></g:plusone></div><p>There are a few ways to stop direct access of a page. I will go over a few simple techniques that will help out those of you that are just getting into PHP.</p>
<h2>Method number 1</h2>
<p>Assuming that all of your pages require a function that was made by yourself and is brought in from another page. ie: &#8220;require_once(&#8216;functions.php&#8217;);&#8221; then we can just do a simple check for the function.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'yourfunction'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/index.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Page cannot be accessed directly.'</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;">?&gt;</span></pre></td></tr></table></div>

<h2>Method 2</h2>
<p>This way of checking for the page is usually a good idea for pages that handle process information. Whether that information is handled through get or post you will still be able to use this form factor.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'location: index.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'You cannot access this page directly.'</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;">?&gt;</span></pre></td></tr></table></div>

<p>Just a little note for using GET and POST information. PHP is a lot more strict then people will give it credit for and this is because most of the time the errors are small and turned off. If you were to take a look at your error log you will get an entry whenever a page loads a variable with GET or POST and said GET or POST does not exist. For this reason you should check if it is set prior to loading the content into any variable.</p>
<h2>Summary of Methods</h3>
<p>In this short tutorial we have gone over how to prevent page access. Although these are good clean ways to prevent users from seeing pages that you don&#8217;t want them to see it is often easier to modify your HTACCESS page to prevent a page from being access directly a quick way would be do add this your .htaccess file.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="htaccess" style="font-family:monospace;">&lt;files process.class.php&gt;
 order allow,deny
 deny from all
&lt;/files&gt;</pre></td></tr></table></div>

<img src="http://codewithdesign.com/?ak_action=api_record_view&id=463&type=feed" alt="" /><p>Related posts:<ol>
<li><a href='http://codewithdesign.com/2010/09/24/creating-a-multi-page-site-from-one-page-using-get/' rel='bookmark' title='Creating a Multi-Page Site From One Page Using GET'>Creating a Multi-Page Site From One Page Using GET</a></li>
<li><a href='http://codewithdesign.com/2009/09/29/the-beginners-guide-to-php/' rel='bookmark' title='The Beginners Guide to PHP'>The Beginners Guide to PHP</a></li>
<li><a href='http://codewithdesign.com/2011/05/30/redundant-data-class-in-php-theory/' rel='bookmark' title='Redundant Data Class in PHP {Theory}'>Redundant Data Class in PHP {Theory}</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://codewithdesign.com/2010/12/28/stop-direct-page-access-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Check Email Function PHP</title>
		<link>http://codewithdesign.com/2010/09/24/check-email-function-php/</link>
		<comments>http://codewithdesign.com/2010/09/24/check-email-function-php/#comments</comments>
		<pubDate>Fri, 24 Sep 2010 19:01:05 +0000</pubDate>
		<dc:creator>Caleb Jonasson</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[check]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[function]]></category>

		<guid isPermaLink="false">http://codewithdesign.com/?p=450</guid>
		<description><![CDATA[TweetJust recently created this piece of code for dream in codes new framework. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 function check_email&#40;$email, $rec = 'MX'&#41;&#123; // 1. clean up email [...]
Related posts:<ol>
<li><a href='http://codewithdesign.com/2009/12/20/random-images-using-a-php-function-cj_random_image/' rel='bookmark' title='Random Images Using A PHP Function: cj_random_image'>Random Images Using A PHP Function: cj_random_image</a></li>
<li><a href='http://codewithdesign.com/2011/06/21/url-authentication-a-new-approach/' rel='bookmark' title='URL Authentication &#8211; A New Approach'>URL Authentication &#8211; A New Approach</a></li>
<li><a href='http://codewithdesign.com/2010/12/28/stop-direct-page-access-with-php/' rel='bookmark' title='Stop Direct Page Access With PHP'>Stop Direct Page Access With PHP</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div style = 'float:left; margin-right:5px;'>            <a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" data-text="Check Email Function PHP" data-via="" data-url="http://codewithdesign.com/2010/09/24/check-email-function-php/" en>Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div class="plus-one-wrap"><g:plusone size="tall" href="http://codewithdesign.com/2010/09/24/check-email-function-php/"></g:plusone></div><p>Just recently created this piece of code for dream in codes new framework.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> check_email<span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #339933;">,</span> <span style="color: #000088;">$rec</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'MX'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">//	1. clean up email</span>
	<span style="color: #000088;">$email</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$v1</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strstr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">' '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v1</span> <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'There is a space.'</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">//	2. check if it has the proper characters</span>
	<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$start</span><span style="color: #339933;">,</span> <span style="color: #000088;">$domain</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;@&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$start</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$domain</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">eregi</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span>  <span style="color: #0000ff;">&quot;good&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// 3. split email into two and check dns</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$domain</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$rec</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">''</span> <span style="color: #009900;">&#41;</span> <span style="color: #000088;">$rec</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;MX&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #990000;">exec</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;nslookup -type=<span style="color: #006699; font-weight: bold;">$recType</span> <span style="color: #006699; font-weight: bold;">$hostName</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">// check each line to find the one that starts with the host</span>
			<span style="color: #666666; font-style: italic;">// name. If it exists then the function succeeded.</span>
			<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$line</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">eregi</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;^<span style="color: #006699; font-weight: bold;">$hostName</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$line</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;hostname is good&quot;</span><span style="color: #339933;">;</span>
					<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #666666; font-style: italic;">// otherwise there was no mail handler for the domain</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The code isn&#8217;t exactly rocket science and thus I won&#8217;t be leaving you with any more information on this function.</p>
<img src="http://codewithdesign.com/?ak_action=api_record_view&id=450&type=feed" alt="" /><p>Related posts:<ol>
<li><a href='http://codewithdesign.com/2009/12/20/random-images-using-a-php-function-cj_random_image/' rel='bookmark' title='Random Images Using A PHP Function: cj_random_image'>Random Images Using A PHP Function: cj_random_image</a></li>
<li><a href='http://codewithdesign.com/2011/06/21/url-authentication-a-new-approach/' rel='bookmark' title='URL Authentication &#8211; A New Approach'>URL Authentication &#8211; A New Approach</a></li>
<li><a href='http://codewithdesign.com/2010/12/28/stop-direct-page-access-with-php/' rel='bookmark' title='Stop Direct Page Access With PHP'>Stop Direct Page Access With PHP</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://codewithdesign.com/2010/09/24/check-email-function-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a Multi-Page Site From One Page Using GET</title>
		<link>http://codewithdesign.com/2010/09/24/creating-a-multi-page-site-from-one-page-using-get/</link>
		<comments>http://codewithdesign.com/2010/09/24/creating-a-multi-page-site-from-one-page-using-get/#comments</comments>
		<pubDate>Fri, 24 Sep 2010 15:43:20 +0000</pubDate>
		<dc:creator>Caleb Jonasson</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[video]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[GET]]></category>
		<category><![CDATA[multi-page]]></category>
		<category><![CDATA[site]]></category>
		<category><![CDATA[url]]></category>

		<guid isPermaLink="false">http://codewithdesign.com/?p=444</guid>
		<description><![CDATA[Tweet Creating a Multi-Page Site From One Page Using GET from Caleb Jonasson on Vimeo. Source Code Download Zip File 1 2 3 4 5 6 7 8 9 10 11 12 13 &#60;?php &#160; $t = $_GET&#91;'t'&#93;; &#160; if&#40;$t == 'contact'&#41;&#123; echo 'contact page'; &#125;else if&#40;$t == 'about'&#41;&#123; echo 'about page'; &#125;else&#123; echo 'index [...]
Related posts:<ol>
<li><a href='http://codewithdesign.com/2010/09/17/using-sessions-to-handle-errors-with-php/' rel='bookmark' title='Using Sessions To Handle Errors With PHP'>Using Sessions To Handle Errors With PHP</a></li>
<li><a href='http://codewithdesign.com/2010/05/18/recycle-pages-by-splitting-your-website-into-pieces-with-php/' rel='bookmark' title='Recycle pages by splitting your website into pieces with PHP'>Recycle pages by splitting your website into pieces with PHP</a></li>
<li><a href='http://codewithdesign.com/2009/10/12/into-to-html-creating-a-basic-web-page/' rel='bookmark' title='Into to HTML: Creating A Basic Web Page'>Into to HTML: Creating A Basic Web Page</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div style = 'float:left; margin-right:5px;'>            <a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" data-text="Creating a Multi-Page Site From One Page Using GET" data-via="" data-url="http://codewithdesign.com/2010/09/24/creating-a-multi-page-site-from-one-page-using-get/" en>Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div class="plus-one-wrap"><g:plusone size="tall" href="http://codewithdesign.com/2010/09/24/creating-a-multi-page-site-from-one-page-using-get/"></g:plusone></div><p><iframe src="http://player.vimeo.com/video/15232211?portrait=0" width="651" height="366" frameborder="0"></iframe>
<p><a href="http://vimeo.com/15232211">Creating a Multi-Page Site From One Page Using GET</a> from <a href="http://vimeo.com/calebj">Caleb Jonasson</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<h2>Source Code</h2>
<p><a href="http://codewithdesign.com/wp-content/uploads/2010/09/multipage_site_from_single_file.zip">Download Zip File</a></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$t</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'t'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$t</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'contact'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'contact page'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$t</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'about'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'about page'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'index page'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;br&gt; &lt;a href=&quot;?t=about&quot;&gt;about&lt;/a&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<img src="http://codewithdesign.com/?ak_action=api_record_view&id=444&type=feed" alt="" /><p>Related posts:<ol>
<li><a href='http://codewithdesign.com/2010/09/17/using-sessions-to-handle-errors-with-php/' rel='bookmark' title='Using Sessions To Handle Errors With PHP'>Using Sessions To Handle Errors With PHP</a></li>
<li><a href='http://codewithdesign.com/2010/05/18/recycle-pages-by-splitting-your-website-into-pieces-with-php/' rel='bookmark' title='Recycle pages by splitting your website into pieces with PHP'>Recycle pages by splitting your website into pieces with PHP</a></li>
<li><a href='http://codewithdesign.com/2009/10/12/into-to-html-creating-a-basic-web-page/' rel='bookmark' title='Into to HTML: Creating A Basic Web Page'>Into to HTML: Creating A Basic Web Page</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://codewithdesign.com/2010/09/24/creating-a-multi-page-site-from-one-page-using-get/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding The &#8216;Like&#8217; Button to WordPress</title>
		<link>http://codewithdesign.com/2010/09/23/adding-the-like-button-to-wordpress/</link>
		<comments>http://codewithdesign.com/2010/09/23/adding-the-like-button-to-wordpress/#comments</comments>
		<pubDate>Thu, 23 Sep 2010 18:41:55 +0000</pubDate>
		<dc:creator>Caleb Jonasson</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[facebook like]]></category>
		<category><![CDATA[installing like button]]></category>
		<category><![CDATA[theme like button]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://codewithdesign.com/?p=436</guid>
		<description><![CDATA[TweetPlugins can often slow down WordPress&#8217; load times so it best to build into the theme if possible. This is why we are going to run through a quick tutorial were we will be adding a like button to a WordPress theme. Getting The Like Button Code The first thing that you are going to [...]
Related posts:<ol>
<li><a href='http://codewithdesign.com/2011/06/29/googles-plus-1-button-integration-and-options/' rel='bookmark' title='Google&#8217;s Plus 1 Button Integration And Options'>Google&#8217;s Plus 1 Button Integration And Options</a></li>
<li><a href='http://codewithdesign.com/2010/05/22/creating-multiple-sidebar-menus-in-wordpress/' rel='bookmark' title='Creating multiple sidebar menus in wordpress'>Creating multiple sidebar menus in wordpress</a></li>
<li><a href='http://codewithdesign.com/2009/12/30/creating-a-top-button/' rel='bookmark' title='Creating a top button'>Creating a top button</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div style = 'float:left; margin-right:5px;'>            <a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" data-text="Adding The &#8216;Like&#8217; Button to WordPress" data-via="" data-url="http://codewithdesign.com/2010/09/23/adding-the-like-button-to-wordpress/" en>Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div class="plus-one-wrap"><g:plusone size="tall" href="http://codewithdesign.com/2010/09/23/adding-the-like-button-to-wordpress/"></g:plusone></div><p>Plugins can often slow down WordPress&#8217; load times so it best to build into the theme if possible. This is why we are going to run through a quick tutorial were we will be adding a like button to a WordPress theme.</p>
<h2>Getting The Like Button Code</h2>
<p>The first thing that you are going to need is the like button code itself. I will be providing it for you but if you would like to go and get it yourself there are some more customization options by <a href="http://developers.facebook.com/docs/reference/plugins/like">going to the Facebook developer page.</a></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>iframe src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://www.facebook.com/widgets/like.php?href=http://skorg.org/&quot;</span>
scrolling<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;no&quot;</span> frameborder<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;0&quot;</span>
style<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;border:none; width:480px; height:80px&quot;</span><span style="color: #339933;">&gt;&lt;/</span>iframe<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Now we need to find a good place for our like button to go. I would recommend placing it below the post so once the user has completed reading through the article they can then like it. If you put it above there is a chance that they are going to miss the button and just move on.</p>
<p>The button is going to be added to the bottom of the post on the single.php page that you can find in your theme directory. Just go to &#8216;/wp-content/themes/your-theme-name/single.php&#8217;.</p>
<p>Now we will simply add the code in and tweak a couple of things so it will work on each page.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&lt;iframe src=&quot;http://www.facebook.com/widgets/like.php?href=<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_permalink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;
scrolling=&quot;no&quot; frameborder=&quot;0&quot;
style=&quot;border:none; width:480px; height:80px&quot;&gt;&lt;/iframe&gt;</pre></td></tr></table></div>

<h2>Adding the Code to Your Theme</h2>
<p>The code will be inserted and your single page should look something more like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&lt;div class=&quot;clear&quot;&gt;&lt;/div&gt;   
	&lt;div class=&quot;meta-entry clear&quot;&gt;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span>
			<span style="color: #666666; font-style: italic;">//full post</span>
        	the_content<span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'read more...'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'skorg-cwd1'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">?&gt;</span>
        &lt;iframe src=&quot;http://www.facebook.com/widgets/like.php?href=<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_permalink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;
        scrolling=&quot;no&quot; frameborder=&quot;0&quot;
        style=&quot;border:none; width:480px; height:80px&quot;&gt;&lt;/iframe&gt;
&nbsp;
       	<span style="color: #000000; font-weight: bold;">&lt;?php</span> edit_post_link<span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Edit!'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'skorg-cwd1'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
        <span style="color: #000000; font-weight: bold;">&lt;?php</span> wp_link_pages<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    &lt;/div&gt;&lt;!--end entry--&gt;
	&lt;div class=&quot;post-footer&quot;&gt;</pre></td></tr></table></div>

<h2>Notes</h2>
<p>When inserting the post just look for something call the_content() then simply past the code bellow this and your like button should work without a flaw.</p>
<img src="http://codewithdesign.com/?ak_action=api_record_view&id=436&type=feed" alt="" /><p>Related posts:<ol>
<li><a href='http://codewithdesign.com/2011/06/29/googles-plus-1-button-integration-and-options/' rel='bookmark' title='Google&#8217;s Plus 1 Button Integration And Options'>Google&#8217;s Plus 1 Button Integration And Options</a></li>
<li><a href='http://codewithdesign.com/2010/05/22/creating-multiple-sidebar-menus-in-wordpress/' rel='bookmark' title='Creating multiple sidebar menus in wordpress'>Creating multiple sidebar menus in wordpress</a></li>
<li><a href='http://codewithdesign.com/2009/12/30/creating-a-top-button/' rel='bookmark' title='Creating a top button'>Creating a top button</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://codewithdesign.com/2010/09/23/adding-the-like-button-to-wordpress/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL: One To Many Using Inner Join</title>
		<link>http://codewithdesign.com/2010/09/22/sql-one-to-many-using-inner-join/</link>
		<comments>http://codewithdesign.com/2010/09/22/sql-one-to-many-using-inner-join/#comments</comments>
		<pubDate>Wed, 22 Sep 2010 17:02:42 +0000</pubDate>
		<dc:creator>Caleb Jonasson</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[inner join]]></category>
		<category><![CDATA[join]]></category>
		<category><![CDATA[One To Many]]></category>
		<category><![CDATA[query]]></category>

		<guid isPermaLink="false">http://codewithdesign.com/?p=425</guid>
		<description><![CDATA[TweetIn this tutorial will be going through it as though we were about to create a browser game that involves units, players, stats, etc. How Does an Inner Join Work? Before we get to far into this and start making our own tables you need to first know how the tables are going to do [...]
Related posts:<ol>
<li><a href='http://codewithdesign.com/2011/04/12/sql-using-unions-in-queries/' rel='bookmark' title='SQL: Using Unions in Queries'>SQL: Using Unions in Queries</a></li>
<li><a href='http://codewithdesign.com/2011/05/30/redundant-data-class-in-php-theory/' rel='bookmark' title='Redundant Data Class in PHP {Theory}'>Redundant Data Class in PHP {Theory}</a></li>
<li><a href='http://codewithdesign.com/2010/05/23/sql-%e2%80%93-an-introduction-to-keys/' rel='bookmark' title='SQL – An introduction to Keys'>SQL – An introduction to Keys</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div style = 'float:left; margin-right:5px;'>            <a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" data-text="SQL: One To Many Using Inner Join" data-via="" data-url="http://codewithdesign.com/2010/09/22/sql-one-to-many-using-inner-join/" en>Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div class="plus-one-wrap"><g:plusone size="tall" href="http://codewithdesign.com/2010/09/22/sql-one-to-many-using-inner-join/"></g:plusone></div><p>In this tutorial will be going through it as though we were about to create a browser game that involves units, players, stats, etc.</p>
<h2>How Does an Inner Join Work?</h2>
<p>Before we get to far into this and start making our own tables you need to first know how the tables are going to do what they do.  We are going to be using a join to connect two tables, this join is called an inner join, with this join type we will be able to link the tables together on a similar column. In this case it will be an ID.</p>
<p>In this game we are going to be needing some players and some units that they will be able to control. So, from within the units table we will have an id that will match the players id. This will allow us to merge the tables on a specific column.</p>
<h2>What Will the Inner Join Tables Look Like?</h2>
<p>Before we can get to the inner join table we will first need the players table. This will have a name, password and id. The id will have to be auto incremental in order to keep track of the id&#8217;s and keep them all unique.</p>
<p><a href="http://codewithdesign.com/wp-content/uploads/2010/09/players.png" rel="lightbox[425]"><img class="alignnone size-full wp-image-428" src="http://codewithdesign.com/wp-content/uploads/2010/09/players.png" alt="" width="227" height="91" /></a></p>
<p>Now that we have our users table we will need to merge them onto the units table to keep track of what units the player owns. The inner join will be performed on the user_id on the table below to the id on the table above.</p>
<p><a href="http://codewithdesign.com/wp-content/uploads/2010/09/units.png" rel="lightbox[425]"><img class="alignnone size-full wp-image-427" src="http://codewithdesign.com/wp-content/uploads/2010/09/units.png" alt="" width="225" height="105" /></a></p>
<p>Once the tables are merged together you will get the following results.</p>
<p><a href="http://codewithdesign.com/wp-content/uploads/2010/09/merged.png" rel="lightbox[425]"><img class="alignnone size-full wp-image-426" src="http://codewithdesign.com/wp-content/uploads/2010/09/merged.png" alt="" width="376" height="105" /></a></p>
<p>As you can see the table now consists of multiple players and the units. This method of table construction can be very powerful since you can then do another inner join from the units table to the singular unit table. This will allow you to link the individual health, strength, etc of a unit to the player.</p>
<h2>SQL Query of an Inner Join</h2>
<p>The sql query looks a little odd when you first look at it but once you go through what is going on you will begin to understand it a lot better.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span>
p.id p_id<span style="color: #000033;">,</span> p.name p_name<span style="color: #000033;">,</span>
u.user_id u_user_id<span style="color: #000033;">,</span> u.unit_id u_unit_id<span style="color: #000033;">,</span> u.id u_id
<span style="color: #990099; font-weight: bold;">FROM</span> players p
<span style="color: #990099; font-weight: bold;">INNER</span> <span style="color: #990099; font-weight: bold;">JOIN</span> units u <span style="color: #990099; font-weight: bold;">ON</span> p_id <span style="color: #CC0099;">=</span> u_user_id</pre></td></tr></table></div>

<p>You can also select all of the users based on the user id. This will involve using the previous statement but it will just require you to add the &#8220;WHERE&#8221; statement. Your code will now look like this.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span>
p.id p_id<span style="color: #000033;">,</span> p.name p_name<span style="color: #000033;">,</span>
u.user_id u_user_id<span style="color: #000033;">,</span> u.unit_id u_unit_id<span style="color: #000033;">,</span> u.id u_id
<span style="color: #990099; font-weight: bold;">FROM</span> players p
<span style="color: #990099; font-weight: bold;">INNER</span> <span style="color: #990099; font-weight: bold;">JOIN</span> units u <span style="color: #990099; font-weight: bold;">ON</span> p_id <span style="color: #CC0099;">=</span> u_user_id
<span style="color: #990099; font-weight: bold;">WHERE</span> u_id <span style="color: #CC0099;">=</span> <span style="color: #008000;">'54'</span></pre></td></tr></table></div>

<p>This is now where the true one to many statement happens. one player to many units. And if you actually work on a game like this you can expand onwards and use a one to one unit or many to many using linking tables.</p>
<h2>Query One To Many Using PHP</h2>
<p>In order to query a one to many using PHP will just need to use the following code.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//store the sql query in the variable $sql</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT
p.id p_id, p.name p_name,
u.user_id u_user_id, u.unit_id u_unit_id, u.id u_id
FROM players p
INNER JOIN units u ON p_id = u_user_id
WHERE u_id = '2'&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//query the database and store the results into $t1. If query fails display the error and kill the page.</span>
<span style="color: #000088;">$t1</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span>or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//This will loop through the results and display them for you</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$t1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'unit id: '</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'u_unit_id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;br&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>Additional Notes:</h2>
<p>When you are creating the tables keep in mind that every id field should be stored as an integer and should match the limit. If you expect to have a large game down the road make sure that all of your id&#8217;s have a length of 11 or 15. That number is up to you but it is best to keep them all at the same length.</p>
<img src="http://codewithdesign.com/?ak_action=api_record_view&id=425&type=feed" alt="" /><p>Related posts:<ol>
<li><a href='http://codewithdesign.com/2011/04/12/sql-using-unions-in-queries/' rel='bookmark' title='SQL: Using Unions in Queries'>SQL: Using Unions in Queries</a></li>
<li><a href='http://codewithdesign.com/2011/05/30/redundant-data-class-in-php-theory/' rel='bookmark' title='Redundant Data Class in PHP {Theory}'>Redundant Data Class in PHP {Theory}</a></li>
<li><a href='http://codewithdesign.com/2010/05/23/sql-%e2%80%93-an-introduction-to-keys/' rel='bookmark' title='SQL – An introduction to Keys'>SQL – An introduction to Keys</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://codewithdesign.com/2010/09/22/sql-one-to-many-using-inner-join/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

