<?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/cat/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://codewithdesign.com</link>
	<description>web application development blog by Caleb Jonasson</description>
	<lastBuildDate>Thu, 15 Sep 2011 17:52:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Statistics &#8211; Simple Logging Design</title>
		<link>http://codewithdesign.com/2011/08/04/statistics-simple-logging-design/</link>
		<comments>http://codewithdesign.com/2011/08/04/statistics-simple-logging-design/#comments</comments>
		<pubDate>Fri, 05 Aug 2011 02:49:50 +0000</pubDate>
		<dc:creator>Caleb Jonasson</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[application design]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[samples]]></category>
		<category><![CDATA[statistics]]></category>

		<guid isPermaLink="false">http://codewithdesign.com/?p=574</guid>
		<description><![CDATA[TweetIntroduction This article is the first in a series focussed on Statistics, Logging and Graphing web application events and information When creating a web application it is important to keep track of everything a user does. Some people may think that this is a little over the top but the more information we can gather [...]


Related posts:<ol><li><a href='http://codewithdesign.com/2011/05/30/redundant-data-class-in-php-theory/' rel='bookmark' title='Permanent Link: Redundant Data Class in PHP {Theory}'>Redundant Data Class in PHP {Theory}</a></li>
<li><a href='http://codewithdesign.com/2011/07/02/password-recovery-theory/' rel='bookmark' title='Permanent Link: Password Recovery {Theory}'>Password Recovery {Theory}</a></li>
<li><a href='http://codewithdesign.com/2009/10/04/creating-a-single-page-login-%e2%80%93-design-view/' rel='bookmark' title='Permanent Link: Creating a Single Page login – Design View'>Creating a Single Page login – Design View</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="Statistics &#8211; Simple Logging Design" data-via="" data-url="http://codewithdesign.com/2011/08/04/statistics-simple-logging-design/" en>Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><g:plusone size="tall" href="http://codewithdesign.com/2011/08/04/statistics-simple-logging-design/"></g:plusone><h2>Introduction</h2>
<p>This article is the first in a series focussed on Statistics, Logging and Graphing web application events and information</p>
<p>When creating a web application it is important to keep track of everything a user does. Some people may think that this is a little over the top but the more information we can gather within a web application the better. It also allows simple and easy moderation practices for those that are moderators within the application.</p>
<h2>Pre-Planning</h2>
<p>This is by far the most important stage in development. During this stage you are going to need to create proper data structures that in theory will not have to be change (although this is extremely rare.) This means that you are going to create the general database that will handle most of the current up to date information and another section that will be used for logging.</p>
<p>Within the main database we are going to have simple things like a user table, a user settings table, a user profile table and a user information table. On tables that hold general information it is important to add a time stamp column that holds the value of last modified. The user should have a column that tells us when the users profile was created. These values aren’t going to really help us when gathering information for a single user simply because the sample of data that we can compare it to is so small. Fortunately these simple values are excellent when we need to pull up values quickly in our system to display user information.</p>
<h2>Single User Sample</h2>
<p>The logging database is going to help us create a large sample of a single user. This is where we are going to constantly insert information and never delete or modify the records. In one of these tables we can store required values that will tell us when a user has had one of their comments deleted by a moderator. Within this table we are going to log the moderator’s id, the user’s id, the time stamp that this action was performed on. and a numeric value that reflects on the reason that the comment was removed. With this data we can then count the comments that are still alive and well and count the comments that were deleted. With this information we can divide the comments that are still alive and well by the comments that were deleted thus giving us a ratio that we can work with.</p>
<p><strong>Note:</strong> It is a good idea to keep track of numbers like this as well and when the ratio was calculated. This will allow us to track user behaviour.</p>
<h2>Multiple User Sample</h2>
<p>Given that we could have a ratio of how well the user follows rules when it comes to commenting on things and taking part in discussion we filter out the users that are not contributing to the community/application. We can also sort users by this ratio and from there bury into their statistics, activities and logs.</p>
<p>Because we have logged all of the individual comments from this user we can closely examine the user interactions by graphing comments, removed comment, comments removed for x</p>
<img src="http://codewithdesign.com/?ak_action=api_record_view&id=574&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://codewithdesign.com/2011/05/30/redundant-data-class-in-php-theory/' rel='bookmark' title='Permanent Link: Redundant Data Class in PHP {Theory}'>Redundant Data Class in PHP {Theory}</a></li>
<li><a href='http://codewithdesign.com/2011/07/02/password-recovery-theory/' rel='bookmark' title='Permanent Link: Password Recovery {Theory}'>Password Recovery {Theory}</a></li>
<li><a href='http://codewithdesign.com/2009/10/04/creating-a-single-page-login-%e2%80%93-design-view/' rel='bookmark' title='Permanent Link: Creating a Single Page login – Design View'>Creating a Single Page login – Design View</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://codewithdesign.com/2011/08/04/statistics-simple-logging-design/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Password Recovery {Theory}</title>
		<link>http://codewithdesign.com/2011/07/02/password-recovery-theory/</link>
		<comments>http://codewithdesign.com/2011/07/02/password-recovery-theory/#comments</comments>
		<pubDate>Sun, 03 Jul 2011 04:02:16 +0000</pubDate>
		<dc:creator>Caleb Jonasson</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[password recovery]]></category>
		<category><![CDATA[password reset]]></category>
		<category><![CDATA[theory]]></category>

		<guid isPermaLink="false">http://codewithdesign.com/?p=567</guid>
		<description><![CDATA[TweetPassword Recovery is a must have in any web application and you as a software engineer need to make sure that you handle this process properly. There are two methods that I like to use but in this article I will only be using one of them. Before you start programming it is a good [...]


Related posts:<ol><li><a href='http://codewithdesign.com/2011/05/30/redundant-data-class-in-php-theory/' rel='bookmark' title='Permanent Link: 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='Permanent Link: SQL – An introduction to Keys'>SQL – An introduction to Keys</a></li>
<li><a href='http://codewithdesign.com/2011/04/12/password-protecting-your-directory-with-htaccess-htpasswd/' rel='bookmark' title='Permanent Link: Password Protecting Your Directory With .htaccess &#038; .htpasswd'>Password Protecting Your Directory With .htaccess &#038; .htpasswd</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="Password Recovery {Theory}" data-via="" data-url="http://codewithdesign.com/2011/07/02/password-recovery-theory/" en>Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><g:plusone size="tall" href="http://codewithdesign.com/2011/07/02/password-recovery-theory/"></g:plusone><p>Password Recovery is a must have in any web application and you as a software engineer need to make sure that you handle this process properly. There are two methods that I like to use but in this article I will only be using one of them. Before you start programming it is a good idea to go through some of the larger web applications to see what they are doing. You may want to modify your process.</p>
<p>Before we begin I should probably make sure that you know just what you are in for before trying this. You are going to need to have knowledge of a server side language, the ability to send mail on your server, store Cookies and Sessions, along with having a running database with access to user emails as a unique field in the database.</p>
<h2>Stage 1</h2>
<p>The password recovery system is going to first require the user to input their email and submit it for checking. On this processing step we will do the following.<br />
1. Check that the email follows proper formatting and there are no bad characters.<br />
2. Check the email against the database to make sure that the user does exist and they have an active account.<br />
3. Check the password recovery table to make sure that the user has not had their password reset in the last 15 minutes.</p>
<p>Now that we have checked and bypassed anything that will put a large hold on the password recovery process we can move on to actually storing the information and content that is required to reset the users password.</p>
<p>Because we are going to be resetting the users password we need to make sure that the user supplied us with a proper email. Even if they did not we are going to show a success screen saying that an email has been sent. This way a bot that is entering in randomness to get emails will not be able to find them this way since everything will return true.</p>
<p>Note: Brute Force should be check for on all forum submissions and thus a lock out system should be added but that is a whole other article.</p>
<p>We now need to generate the items that are going to be stored. Because we will be storing information in a cookie and inside of a session. I know guys that store two keys but I just store a time stamp in the database, break it up into segments, add alpahnumeric characters and hash the information. This way I can keep a time stamp in the database and keep the information secretly stored in a session specific to the user.</p>
<p>Note: When storing time only use the generating function once and store the value into a variable so we have the same time when committing to the database.</p>
<p>Now that we have the information set into the appropriate variables we can store the information via insert to a table that holds the user id, key and time stamp. This way we can track our two keys and a time stamp of when they tried to reset their password. If we wish to lock the user out of the system for a certain amount of time we can simply change the key to null and check for this. If the (current time &#8211; date stored) < (15 * 60) and there is no key then we can just display an error message.</p>
<p>When everything is properly stored into the database we can send the user an email. This email will contain a link to the password recovery page along with a key which will be the one saved in the cookie. This way we can pass two keys back and know that the only way for this user to be the wrong user is if they have the email account as well.</p>
<h2>Stage 2</h2>
<p>This page is going to check for a key that is alphanumeric and of a certain length. With this key we are going to check to make sure it is the same as the cookie and that it is in the database. If the user is in the database we can pull the rest of the information including their email from the other table to make sure that we are able to regenerate one of their keys assuming that we used it.</p>
<p>Finally we need to make sure that the generated key matches the one that is stored inside of the session and the date is within the allotted time. From here we need to first delete the record in the database, generate a random password, hash it and store it into the system, and send them a copy of this new password via email.</p>
<p>During this last portion you need to handle the case that the page was accessed with improper values or missing values. eg: if the user does not have the cookie set or the time is out of range we need to handle the case and remove the key from the database so we know that that user won&#8217;t be able to try and reset their password for another 15 minutes. You can also bump up the time that is stored in the database depending on what was missing in case of a bad attempt which will allow you to easily lock a user out.</p>
<img src="http://codewithdesign.com/?ak_action=api_record_view&id=567&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://codewithdesign.com/2011/05/30/redundant-data-class-in-php-theory/' rel='bookmark' title='Permanent Link: 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='Permanent Link: SQL – An introduction to Keys'>SQL – An introduction to Keys</a></li>
<li><a href='http://codewithdesign.com/2011/04/12/password-protecting-your-directory-with-htaccess-htpasswd/' rel='bookmark' title='Permanent Link: Password Protecting Your Directory With .htaccess &#038; .htpasswd'>Password Protecting Your Directory With .htaccess &#038; .htpasswd</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://codewithdesign.com/2011/07/02/password-recovery-theory/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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/2011/07/02/password-recovery-theory/' rel='bookmark' title='Permanent Link: Password Recovery {Theory}'>Password Recovery {Theory}</a></li>
<li><a href='http://codewithdesign.com/2010/09/24/check-email-function-php/' rel='bookmark' title='Permanent Link: Check Email Function PHP'>Check Email Function PHP</a></li>
<li><a href='http://codewithdesign.com/2011/05/03/htaccess-handling-bad-server-requests/' rel='bookmark' title='Permanent Link: .htaccess Handling Bad Server Requests'>.htaccess Handling Bad Server Requests</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><g:plusone size="tall" href="http://codewithdesign.com/2011/06/21/url-authentication-a-new-approach/"></g:plusone><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/2011/07/02/password-recovery-theory/' rel='bookmark' title='Permanent Link: Password Recovery {Theory}'>Password Recovery {Theory}</a></li>
<li><a href='http://codewithdesign.com/2010/09/24/check-email-function-php/' rel='bookmark' title='Permanent Link: Check Email Function PHP'>Check Email Function PHP</a></li>
<li><a href='http://codewithdesign.com/2011/05/03/htaccess-handling-bad-server-requests/' rel='bookmark' title='Permanent Link: .htaccess Handling Bad Server Requests'>.htaccess Handling Bad Server Requests</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='Permanent Link: Password Recovery {Theory}'>Password Recovery {Theory}</a></li>
<li><a href='http://codewithdesign.com/2011/04/07/create-your-own-phpamf-data-service-for-flex/' rel='bookmark' title='Permanent Link: 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/2011/08/04/statistics-simple-logging-design/' rel='bookmark' title='Permanent Link: Statistics &#8211; Simple Logging Design'>Statistics &#8211; Simple Logging Design</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><g:plusone size="tall" href="http://codewithdesign.com/2011/05/30/redundant-data-class-in-php-theory/"></g:plusone><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='Permanent Link: Password Recovery {Theory}'>Password Recovery {Theory}</a></li>
<li><a href='http://codewithdesign.com/2011/04/07/create-your-own-phpamf-data-service-for-flex/' rel='bookmark' title='Permanent Link: 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/2011/08/04/statistics-simple-logging-design/' rel='bookmark' title='Permanent Link: Statistics &#8211; Simple Logging Design'>Statistics &#8211; Simple Logging Design</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/2010/12/28/stop-direct-page-access-with-php/' rel='bookmark' title='Permanent Link: Stop Direct Page Access With PHP'>Stop Direct Page Access With PHP</a></li>
<li><a href='http://codewithdesign.com/2010/03/30/php-increasing-a-scripts-runtime/' rel='bookmark' title='Permanent Link: PHP: Increasing a scripts runtime'>PHP: Increasing a scripts runtime</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><g:plusone size="tall" href="http://codewithdesign.com/2011/05/26/curl-403-error-returning/"></g:plusone><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/2010/12/28/stop-direct-page-access-with-php/' rel='bookmark' title='Permanent Link: Stop Direct Page Access With PHP'>Stop Direct Page Access With PHP</a></li>
<li><a href='http://codewithdesign.com/2010/03/30/php-increasing-a-scripts-runtime/' rel='bookmark' title='Permanent Link: PHP: Increasing a scripts runtime'>PHP: Increasing a scripts runtime</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>PHP: Array Of Bad Words</title>
		<link>http://codewithdesign.com/2011/05/20/php-array-of-bad-words/</link>
		<comments>http://codewithdesign.com/2011/05/20/php-array-of-bad-words/#comments</comments>
		<pubDate>Fri, 20 May 2011 13:16:08 +0000</pubDate>
		<dc:creator>Caleb Jonasson</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[bad words]]></category>
		<category><![CDATA[curse words]]></category>
		<category><![CDATA[racial slurs]]></category>
		<category><![CDATA[word filter]]></category>

		<guid isPermaLink="false">http://codewithdesign.com/?p=534</guid>
		<description><![CDATA[TweetWhen creating applications that are going to be used by hundreds of thousands; it is important to make sure that you have the proper facilities in place to handle curse words that are entered in by users. This can be done by checking an array of bad words. The code is simply&#8230; 1 2 3 [...]


Related posts:<ol><li><a href='http://codewithdesign.com/2009/12/05/looping-with-an-array/' rel='bookmark' title='Permanent Link: Looping with an array'>Looping with an array</a></li>
<li><a href='http://codewithdesign.com/2011/05/03/htaccess-handling-bad-server-requests/' rel='bookmark' title='Permanent Link: .htaccess Handling Bad Server Requests'>.htaccess Handling Bad Server Requests</a></li>
<li><a href='http://codewithdesign.com/2010/09/24/creating-a-multi-page-site-from-one-page-using-get/' rel='bookmark' title='Permanent Link: Creating a Multi-Page Site From One Page Using GET'>Creating a Multi-Page Site From One Page Using GET</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="PHP: Array Of Bad Words" data-via="" data-url="http://codewithdesign.com/2011/05/20/php-array-of-bad-words/" en>Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><g:plusone size="tall" href="http://codewithdesign.com/2011/05/20/php-array-of-bad-words/"></g:plusone><p>When creating applications that are going to be used by hundreds of thousands; it is important to make sure that you have the proper facilities in place to handle curse words that are entered in by users. This can be done by checking an array of bad words.</p>
<p>The code is simply&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</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;">//foul language array</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">badWords</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'word1'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'word2'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'word3'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//Now you just need to go through your string and make comparisons.</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Rather then posting the code directly onto the blog I would rather have a site that is safe for all readers and not be indexed with foul language and racial slurs and thus is why I am offering the array via <a href='http://codewithdesign.com/wp-content/uploads/2011/05/bad-words-php-array.zip'>a text file within a compressed zip</a>.</p>
<img src="http://codewithdesign.com/?ak_action=api_record_view&id=534&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://codewithdesign.com/2009/12/05/looping-with-an-array/' rel='bookmark' title='Permanent Link: Looping with an array'>Looping with an array</a></li>
<li><a href='http://codewithdesign.com/2011/05/03/htaccess-handling-bad-server-requests/' rel='bookmark' title='Permanent Link: .htaccess Handling Bad Server Requests'>.htaccess Handling Bad Server Requests</a></li>
<li><a href='http://codewithdesign.com/2010/09/24/creating-a-multi-page-site-from-one-page-using-get/' rel='bookmark' title='Permanent Link: Creating a Multi-Page Site From One Page Using GET'>Creating a Multi-Page Site From One Page Using GET</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://codewithdesign.com/2011/05/20/php-array-of-bad-words/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP Automatically Include Classes</title>
		<link>http://codewithdesign.com/2011/04/13/php-automatically-include-classes/</link>
		<comments>http://codewithdesign.com/2011/04/13/php-automatically-include-classes/#comments</comments>
		<pubDate>Wed, 13 Apr 2011 21:28:18 +0000</pubDate>
		<dc:creator>Caleb Jonasson</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[automatic]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[loading]]></category>

		<guid isPermaLink="false">http://codewithdesign.com/?p=511</guid>
		<description><![CDATA[TweetPulling In Content Php has a nice small feature that allows us to automatically load classes that we use in our web application. This will check the designated location&#8217;s files and pull in any classes that we reference inside of the application. function __autoload&#40;$class&#41; &#123; include $class . '.php'; &#125; &#160; $car = new carObject&#40;&#41;; [...]


Related posts:<ol><li><a href='http://codewithdesign.com/2011/05/30/redundant-data-class-in-php-theory/' rel='bookmark' title='Permanent Link: Redundant Data Class in PHP {Theory}'>Redundant Data Class in PHP {Theory}</a></li>
<li><a href='http://codewithdesign.com/2011/04/07/create-your-own-phpamf-data-service-for-flex/' rel='bookmark' title='Permanent Link: 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/2009/12/20/random-images-using-a-php-function-cj_random_image/' rel='bookmark' title='Permanent Link: Random Images Using A PHP Function: cj_random_image'>Random Images Using A PHP Function: cj_random_image</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="PHP Automatically Include Classes" data-via="" data-url="http://codewithdesign.com/2011/04/13/php-automatically-include-classes/" en>Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><g:plusone size="tall" href="http://codewithdesign.com/2011/04/13/php-automatically-include-classes/"></g:plusone><h2>Pulling In Content</h2>
<p>Php has a nice small feature that allows us to automatically load classes that we use in our web application. This will check the designated location&#8217;s files and pull in any classes that we reference inside of the application.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> __autoload<span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">include</span> <span style="color: #000088;">$class</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.php'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$car</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> carObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$house</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> houseObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$boat</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> boatObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Pull Classes From A Path</h2>
<p>It is nice to keep all application classes in the same directory. Here is a quick fix to the above code to import everything from the classes folder on the server.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> __autoload<span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'/class/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$class</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.php'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$car</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> carObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$house</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> houseObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$boat</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> boatObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is a small quick fix that works because the include function works with a simple string.</p>
<img src="http://codewithdesign.com/?ak_action=api_record_view&id=511&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://codewithdesign.com/2011/05/30/redundant-data-class-in-php-theory/' rel='bookmark' title='Permanent Link: Redundant Data Class in PHP {Theory}'>Redundant Data Class in PHP {Theory}</a></li>
<li><a href='http://codewithdesign.com/2011/04/07/create-your-own-phpamf-data-service-for-flex/' rel='bookmark' title='Permanent Link: 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/2009/12/20/random-images-using-a-php-function-cj_random_image/' rel='bookmark' title='Permanent Link: Random Images Using A PHP Function: cj_random_image'>Random Images Using A PHP Function: cj_random_image</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://codewithdesign.com/2011/04/13/php-automatically-include-classes/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='Permanent Link: 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='Permanent Link: 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='Permanent Link: 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><g:plusone size="tall" href="http://codewithdesign.com/2011/04/09/getting-secured-using-mysqli/"></g:plusone><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='Permanent Link: 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='Permanent Link: 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='Permanent Link: 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='Permanent Link: Getting Secured Using Mysqli'>Getting Secured Using Mysqli</a></li>
<li><a href='http://codewithdesign.com/2010/09/22/sql-one-to-many-using-inner-join/' rel='bookmark' title='Permanent Link: SQL: One To Many Using Inner Join'>SQL: One To Many Using Inner Join</a></li>
<li><a href='http://codewithdesign.com/2011/04/12/sql-using-unions-in-queries/' rel='bookmark' title='Permanent Link: SQL: Using Unions in Queries'>SQL: Using Unions in Queries</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><g:plusone size="tall" href="http://codewithdesign.com/2011/04/08/searching-with-in-a-prepared-statement/"></g:plusone><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='Permanent Link: Getting Secured Using Mysqli'>Getting Secured Using Mysqli</a></li>
<li><a href='http://codewithdesign.com/2010/09/22/sql-one-to-many-using-inner-join/' rel='bookmark' title='Permanent Link: SQL: One To Many Using Inner Join'>SQL: One To Many Using Inner Join</a></li>
<li><a href='http://codewithdesign.com/2011/04/12/sql-using-unions-in-queries/' rel='bookmark' title='Permanent Link: SQL: Using Unions in Queries'>SQL: Using Unions in Queries</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>Create Your Own PHP/AMF Data Service For Flex</title>
		<link>http://codewithdesign.com/2011/04/07/create-your-own-phpamf-data-service-for-flex/</link>
		<comments>http://codewithdesign.com/2011/04/07/create-your-own-phpamf-data-service-for-flex/#comments</comments>
		<pubDate>Thu, 07 Apr 2011 22:51:26 +0000</pubDate>
		<dc:creator>Caleb Jonasson</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[AMF]]></category>
		<category><![CDATA[custom data service]]></category>
		<category><![CDATA[data service]]></category>

		<guid isPermaLink="false">http://codewithdesign.com/?p=484</guid>
		<description><![CDATA[TweetThis is a quick and short tutorial on how to create your own data services for flex to read. Also, note that in this tutorial we will be focussing on what a PHP Developer would be doing and not what a Flex Developer would do, we are not going to go over setting up data [...]


Related posts:<ol><li><a href='http://codewithdesign.com/2011/06/08/flex-data-service-debugging-software/' rel='bookmark' title='Permanent Link: Flex Data Service Debugging Software'>Flex Data Service Debugging Software</a></li>
<li><a href='http://codewithdesign.com/2011/04/09/getting-secured-using-mysqli/' rel='bookmark' title='Permanent Link: Getting Secured Using Mysqli'>Getting Secured Using Mysqli</a></li>
<li><a href='http://codewithdesign.com/2011/05/30/redundant-data-class-in-php-theory/' rel='bookmark' title='Permanent Link: 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="Create Your Own PHP/AMF Data Service For Flex" data-via="" data-url="http://codewithdesign.com/2011/04/07/create-your-own-phpamf-data-service-for-flex/" en>Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><g:plusone size="tall" href="http://codewithdesign.com/2011/04/07/create-your-own-phpamf-data-service-for-flex/"></g:plusone><p>This is a quick and short tutorial on how to create your own data services for flex to read. Also, note that in this tutorial we will be focussing on what a PHP Developer would be doing and not what a Flex Developer would do, we are not going to go over setting up data services or Flex, we are just going to be writing out classes.</p>
<h2>Classes</h2>
<p>The class is a rather simple thing to write out when focussing on creating an object that flex will be able to easily read. The trick to creating proper services is knowing what flex can do with your functions and your class.</p>
<p>The first thing to know is that flex is not going to be passing any values into the class and the class is not going to behave in a similar fashion to flex. This means that everything you do needs to operate on a function level.</p>
<h2>The Construct</h2>
<p>The construct is going to allow you to store information into the classes variables such as anything else and thus it is a good place to initialize and store a connection.</p>
<p>There isn&#8217;t a standard class declaration line within Flex and thus you cannot simply pass arguments through the parameters of the class.</p>
<p><strong>The construct should look like this:</strong></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;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">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;">'root'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'password'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'databasename'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>Function Error Returns</h2>
<p>Remember that the front end development team is going to need to know how to handle every situation of data that they receive and they will need to handle any scenario. Because of this you are going to need to return a status and message within the object in case anything goes wrong. A common standard is to simply store a boolean of if it worked as the first value returned. If the boolean is a false value then we know that the second object in the array is going to be an error message or an error code letting the front end know what went wrong.</p>
<h2>Function Arguments</h2>
<p>When setting up your arguments in the parameters of a function you are going to need to break away from pre setting the value of the argument with:</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: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getMeta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$metaName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$metaType</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;video&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Values that are pre set in PHP do not show up in flex as an optional parameter.</p>
<h2>Returning the Object</h2>
<p>When using PHP/AMF as a data service it is good likely that you will need to return some rather complex objects to the front end team. The best way to pass data and allow them to use it is to store the information within an multi-dimensional array.</p>
<p>This means that when you use something like a prepared statement, you are going to need to handle the data and not just throw it back in a PHP array. If you throw back an unhandled mysqli array the front end team may kill you, especially if you have joins within your connection. This will force them to recreate an object from a singular one dimensional array.</p>
<h2>Example of a custom Data Service.</h2>

<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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> pullVideoFromUser<span style="color: #009900;">&#40;</span><span style="color: #000088;">$userNid</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//Query the database and pull video information</span>
        <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT t.name tour_name, t.tour_nid video_nid, tm.created tour_created, tm.modified tour_modified, tm.viewcount tour_viewcount
            FROM tbl_user_video ut
            INNER JOIN tbl_video t ON ut.video_nid = t.video_nid
            INNER JOIN tbl_video_meta tm ON tm.video_nid = tu.video_nid
            WHERE u.user_nid = ?&quot;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$videoReturn</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #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;">connect</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: #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;">'i'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$userNid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">store_results</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: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bind_results</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$nid</span><span style="color: #339933;">,</span> <span style="color: #000088;">$created</span><span style="color: #339933;">,</span> <span style="color: #000088;">$modified</span><span style="color: #339933;">,</span> <span style="color: #000088;">$viewcount</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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: #666666; font-style: italic;">//create an object holding the appropriate information</span>
                    <span style="color: #000088;">$videoReturn</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'status'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'key'</span> <span style="color: #339933;">=&gt;</span><span style="color: #000088;">$key</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'nid'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$nid</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'created'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$created</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'modified'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$modified</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'viewcount'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$viewcount</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: #666666; font-style: italic;">//Store a status of false and return an error message within the object.</span>
		<span style="color: #000088;">$videoReturn</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'status'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'There was an issue returning the video.'</span><span style="color: #009900;">&#41;</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: #666666; font-style: italic;">//Store a status of false and return an error message within the object.</span>
	    <span style="color: #000088;">$videoReturn</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'status'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&gt;</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: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
        <span style="color: #666666; font-style: italic;">//return the object</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$videoReturn</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<img src="http://codewithdesign.com/?ak_action=api_record_view&id=484&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://codewithdesign.com/2011/06/08/flex-data-service-debugging-software/' rel='bookmark' title='Permanent Link: Flex Data Service Debugging Software'>Flex Data Service Debugging Software</a></li>
<li><a href='http://codewithdesign.com/2011/04/09/getting-secured-using-mysqli/' rel='bookmark' title='Permanent Link: Getting Secured Using Mysqli'>Getting Secured Using Mysqli</a></li>
<li><a href='http://codewithdesign.com/2011/05/30/redundant-data-class-in-php-theory/' rel='bookmark' title='Permanent Link: Redundant Data Class in PHP {Theory}'>Redundant Data Class in PHP {Theory}</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://codewithdesign.com/2011/04/07/create-your-own-phpamf-data-service-for-flex/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

