<?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>Stephen McIntyre &#187; SQL</title>
	<atom:link href="http://stephenmcintyre.net/category/blog/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://stephenmcintyre.net</link>
	<description>Young Scottish Web Designer</description>
	<lastBuildDate>Thu, 10 Jun 2010 15:25:37 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Randomising Database Output</title>
		<link>http://stephenmcintyre.net/blog/randomising-database-output/</link>
		<comments>http://stephenmcintyre.net/blog/randomising-database-output/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 21:13:47 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=294</guid>
		<description><![CDATA[Collecting a random data set from your database can be useful for all kinds of data driven applications including analytics and &#8220;grey areas&#8221; in video game outcomes  (battles, gambling, etc.).
Here I will discuss the use and methods of randomisation in PHP with data from a MySQL database.
Collecting the Data
First we need to grab our [...]]]></description>
			<content:encoded><![CDATA[<p>Collecting a random data set from your database can be useful for all kinds of data driven applications including analytics and &#8220;grey areas&#8221; in video game outcomes  (battles, gambling, etc.).</p>
<p>Here I will discuss the use and methods of randomisation in PHP with data from a MySQL database.<span id="more-294"></span></p>
<h3>Collecting the Data</h3>
<p>First we need to grab our database rows.</p>
<p>For this we uses a simple <code>SELECT</code> statement, where in this example is taken from a &#8220;users&#8221; table with two fields &#8220;ID&#8221; and &#8220;name&#8221;.</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: #000088;">$db</span> <span style="color: #339933;">=</span> MySQLi<span style="color: #009900;">&#40;</span>DB_HOST<span style="color: #339933;">,</span> DB_USER<span style="color: #339933;">,</span> DB_PASS<span style="color: #339933;">,</span> DB_NAME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT `ID`, `name` FROM `users` ORDER BY `ID` ASC&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$rows</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>
&nbsp;
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$r</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$results</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$rows</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$r</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;ol&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rows</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;li&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ID'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' = '</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/li&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/ol&gt;'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This will print out all of the data from our database into an ordered list, arranged by ID from 1 upwards.</p>
<h3>Randomise with PHP</h3>
<p>To then take this data and shuffle its values, we can then take the conveniently named <a href="http://php.net/shuffle">shuffle</a> method in PHP and apply it to our <code>$rows</code> array.</p>
<p>Add this code to the above script at line 13. It takes the data and again prints it out into an ordered list, this time with a randomised data set.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>13
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">shuffle</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rows</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h3>Using the MySQL Query</h3>
<p>Now, since here we are already using a MySQL query, we can utilise the <a href="http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_rand">RAND()</a> method by adding it to our <code>$query</code> string.</p>
<p>This should be used as an alternative to the PHP shuffle method, so as such should not include the above line with shuffle in it (unless you want to randomise it twice &#8211; but it won&#8217;t make your data any more unique).</p>
<p>Replace your query string with this, on line 3.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT `ID`, `name` FROM `users` ORDER BY RAND()&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Notice it&#8217;s only the last part (<code>ORDER BY...</code>) that changes.</p>
<p>This code &#8211; or at least the idea &#8211; can be applied to, and is useful for, all programming languages. The syntax will vary between them but fundamentally the results will be the same.</p>
<p>Making a Facebook app? Why not award the user with a random item when they log in once a day, or show a rearranged list of their friends if it&#8217;s otherwise too large to show on a page.</p>
<p>Own a blog? Apply this to your homepage and show some old posts. It will also get their page rankings higher if they are being re-indexed by search engines. It&#8217;s like fresh content!</p>
<p>It will also help your visitors find posts they never knew they were looking for. &#8216;Cos we&#8217;re all just browsing after all <img src='http://stephenmcintyre.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/randomising-database-output/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Let Your Database Do The Work For You</title>
		<link>http://stephenmcintyre.net/blog/let-your-database-do-it/</link>
		<comments>http://stephenmcintyre.net/blog/let-your-database-do-it/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 17:00:14 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=149</guid>
		<description><![CDATA[In the below examples I&#8217;ll use data from a fictional database table named &#8220;users&#8221;. We&#8217;re looking to return the 10 most recently added users with a date of birth later than January 1st 1970.

Bad practice
One way I&#8217;ve seen it done before is like this:

$query = mysql_query&#40; &#34;SELECT * FROM `users`&#34; &#41;;
while&#40; $row = mysql_fetch_assoc&#40; $query [...]]]></description>
			<content:encoded><![CDATA[<p>In the below examples I&#8217;ll use data from a fictional database table named &#8220;users&#8221;. We&#8217;re looking to return the 10 most recently added users with a date of birth later than January 1st 1970.<br />
<span id="more-149"></span></p>
<h2>Bad practice</h2>
<p>One way I&#8217;ve seen it done before is like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;SELECT * FROM `users`&quot;</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;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$query</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$dob</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'dob'</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$joined</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'joined'</span><span style="color: #009900;">&#93;</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;">$dob</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;=</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$joined</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #990000;">krsort</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$array</span><span style="color: #339933;">,</span> SORT_NUMERIC <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">array_slice</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$array</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$array</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Name: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Join Date: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'joined'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Date of Birth: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'dob'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Good practice</h2>
<p>The alternative uses some SQL techniques to do all of that data sorting within the database query.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$dob_limit</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'c'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;SELECT `name`,`joined`,`dob` FROM `users` WHERE `dob` &amp;gt;= <span style="color: #006699; font-weight: bold;">$dob_limit</span> ORDER BY `joined` DESC LIMIT 10&quot;</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;">$item</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$query</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;">'Name: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Join Date: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'joined'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Date of Birth: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'dob'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As you can see, the latter script should look far simpler, is easier to write, and works much quicker and efficiently.</p>
<p>Note also that in the second script the fields after the <code>SELECT</code> statement have been <strong>specified</strong> rather than leaving a wild-card asterisk (<code>*</code>). The asterisk returns <strong>all</strong> data in the row that the SQL statement found a match in, and the more data we have in a row the more data could become redundant as we extract it.</p>
<p>For example, consider that the <code>users</code> table had extra fields containing first name, surname, website URL, small personal biography, home town, favourite film, favourite food, and so on. For each row you extract, all seven of those become <strong>unused</strong> pieces of data in the memory on your server and possibly even the computers of your users. Multiply that seven by each row in your database, then by how many times the query will be called, and things start to add up pretty quickly.</p>
<p><a href="http://stephenmcintyre.net/wp-content/uploads/2009/12/db-users.jpg"><img class="aligncenter size-full wp-image-224" title="db-users" src="http://stephenmcintyre.net/wp-content/uploads/2009/12/db-users.jpg" alt="" width="300" height="201" /></a></p>
<p>The point is then that if you start with a <strong>clear idea</strong> on what you actually need in order to use your script, you can be specific in what data you need to collect from your database. Doing so decreases your page load times and lowers your bandwidth, usually with dramatic results.</p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/let-your-database-do-it/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
