<?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>Edge of the Web &#187; mysql</title>
	<atom:link href="http://www.edgeoftheweb.co.uk/blog/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.edgeoftheweb.co.uk/blog</link>
	<description></description>
	<lastBuildDate>Tue, 03 Jan 2012 15:10:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>MySQL PHP data processing &#8211; which is faster?</title>
		<link>http://www.edgeoftheweb.co.uk/blog/2010/12/16/mysql-php-processing/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mysql-php-processing</link>
		<comments>http://www.edgeoftheweb.co.uk/blog/2010/12/16/mysql-php-processing/#comments</comments>
		<pubDate>Thu, 16 Dec 2010 14:32:23 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[data processing]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[develop]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.edgeoftheweb.co.uk/blog/?p=774</guid>
		<description><![CDATA[Many web developers aren't aware that the MySQL database language supports a number of useful function for data processing that are commonly overlooked and replaced with post-processing in PHP. For a recent project I decided to see how much I could achieve using a single MySQL query and as little post-processing with PHP.]]></description>
			<content:encoded><![CDATA[<p>Many web developers aren&#8217;t aware that the MySQL database language supports a number of <a title="MySQL Functions" href="http://dev.mysql.com/doc/refman/5.0/en/func-op-summary-ref.html">useful function for data processing</a> that are commonly overlooked and replaced with post-processing in PHP. For a recent project I decided to see how much I could achieve using a single MySQL query and as little post-processing with PHP.</p>
<p>There were two functions in particular that I made use of in my awesome query:</p>
<p>SUM() and IF()</p>
<p>These functions can be combined and used as a shortcut to provide statistical information without requiring PHP to do the data processing.</p>
<p>For example, in this scenario I needed to return the number of sales that day as well as the number of sales yesterday, and the total amount of money taken from those sales for both days. I could have just run two queries like this:</p>
<pre>SELECT * FROM orders WHERE DATE(date)=CURDATE();
SELECT * FROM orders WHERE DATE(date)=SUB_DATE(CURDATE(), INTERVAL 1 DAY);</pre>
<p>and then counted the number of returned rows using mysql_num_rows() in PHP and looped through those rows in order to add up the sales totals.</p>
<p>Instead, using a combination of SUM() and IF(), I did all the data processing within the MySQL query. This time I&#8217;ve selected sales from this month and last month:</p>
<pre>SELECT SUM(IF(MONTH(date)=MONTH(CURDATE()),price,0)) as "revenue_this_month",
<div style="padding-left: 50px;">SUM(IF(MONTH(date)&lt;&gt;MONTH(CURDATE()),price,0)) as "revenue_last_month",
SUM(MONTH(date)=MONTH(CURDATE())) as "sales_this_month",
SUM(MONTH(date)&lt;&gt;MONTH(CURDATE())) as "sales_last_month"
FROM orders
WHERE MONTH(date)&gt;=MONTH(CURDATE())-1;</div>
</pre>
<p>The way that this query works is by using the MONTH() function to return the month value from a date string e.g. MONTH(&#8220;2010-01-25 12:04:26&#8243;) would return &#8220;1&#8243;. This can then be used to limit orders by month.</p>
<p>The IF() function returns it&#8217;s 2nd argument if the 1st argument evaluates to TRUE, and its 3rd argument if it evaluates to FALSE. Which is brilliant, because we can then determine on which rows returned by the query that our SUM() function will increment. Additionally, as we can rename columns we can also give them useful column titles.</p>
<p>The alternative, as I mentioned above, would be to use PHP to process the data set returned by the following query:</p>
<pre>SELECT price, MONTH(date) as "month"
<div style="padding-left: 50px;">FROM orders WHERE
MONTH(date)&gt;=MONTH(CURDATE())-1;</div>
</pre>
<p>Just out of curiosity, I ran 50,000 tests on two different quad-core Windows 7 machines running a LAMP local server using the queries above on a database with randomly entered dates and prices and found these results on the performance difference:</p>
<table>
<thead>
<tr>
<th>Rows in Database</th>
<th>Rows Returned by Query</th>
<th>Number of times faster MySQL was than PHP</th>
</tr>
</thead>
<tbody>
<tr>
<td>117,000</td>
<td>19,136</td>
<td>13.90</td>
</tr>
<tr>
<td>58,500</td>
<td>9,530</td>
<td>13.63</td>
</tr>
</tbody>
</table>
<p>So, next time you&#8217;re creating summary reports using MySQL it might just be worth having a think to see if you can save yourself a lot of PHP processing by using some of the in-built power of MySQL.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.edgeoftheweb.co.uk/blog/2010/12/16/mysql-php-processing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Don&#8217;t trust your users!</title>
		<link>http://www.edgeoftheweb.co.uk/blog/2009/08/14/never-trust-your-users/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=never-trust-your-users</link>
		<comments>http://www.edgeoftheweb.co.uk/blog/2009/08/14/never-trust-your-users/#comments</comments>
		<pubDate>Fri, 14 Aug 2009 09:05:56 +0000</pubDate>
		<dc:creator>mark</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[application design]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[validating user input]]></category>
		<category><![CDATA[web security]]></category>

		<guid isPermaLink="false">http://www.edgeoftheweb.co.uk/blog/?p=91</guid>
		<description><![CDATA[If there is one thing I&#8217;ve learned since developing websites and applications, it&#8217;s the importance of validating user input. Don&#8217;t trust for one second what your user is sending you. Anytime a user is asked for input, whether it be &#8230;]]></description>
			<content:encoded><![CDATA[<p>If there is one thing I&#8217;ve learned since developing websites and applications, it&#8217;s the importance of validating user input. Don&#8217;t trust for one second what your user is sending you. Anytime a user is asked for input, whether it be their name, email address or an uploaded image, this must be filtered to:</p>
<ol>
<li>Check it is actually from who it&#8217;s supposed to be</li>
<li>Make sure it contains the information you want and is structured correctly</li>
</ol>
<p>Data must never be changed to accommodate mistakes, always tell the user if they have done something incorrectly. Make them play by your rules. Changing incorrect user data can create vulnerabilities.</p>
<p>Once data has be validated, it must be escaped before being inserted into a database. The safest way to make sure data is clean is to set-up a new array() and then put the data through <span><strong>htmlentities()</strong></span> and <span><strong>mysql_real_escape_string().<br />
</strong></span></p>
<p>So never trust your users. Always treat data input as invalid until you can prove otherwise. It sounds harsh, but it&#8217;s the only safe way to protect you, your data and your customers.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.edgeoftheweb.co.uk/blog/2009/08/14/never-trust-your-users/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

