<?xml version="1.0" encoding="iso-8859-1"?>
<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>CrankBerry Blog &#187; Random</title>
	<atom:link href="http://www.crankberryblog.com/tag/random/feed" rel="self" type="application/rss+xml" />
	<link>http://www.crankberryblog.com</link>
	<description>Sharing ideas on web design, development and the internet economy</description>
	<lastBuildDate>Thu, 24 Nov 2011 19:37:08 +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>Random row from your MySQL table with PHP, alternative to ORDER BY rand()</title>
		<link>http://www.crankberryblog.com/2010/random-row-from-your-mysql-table-with-php-alternative-to-order-by-rand</link>
		<comments>http://www.crankberryblog.com/2010/random-row-from-your-mysql-table-with-php-alternative-to-order-by-rand#comments</comments>
		<pubDate>Thu, 22 Apr 2010 21:28:04 +0000</pubDate>
		<dc:creator>Jerry Low</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Script]]></category>
		<category><![CDATA[Random]]></category>

		<guid isPermaLink="false">http://www.crankberryblog.com/?p=379</guid>
		<description><![CDATA[Working with MySQL and PHP is awesome. Its one of the best way to manage your data, but once in a while you may want to do some randomizing. One problem I came across recently was how to select a random row from your MySQL table with PHP. Using the ORDER BY rand() method in your query maybe a bit slow so here's what we've got.]]></description>
			<content:encoded><![CDATA[<p>Working with MySQL and PHP is awesome. Its one of the best way to manage your data, but once in a while you may want to do some randomizing. One problem I came across recently was how to select a random row from your MySQL table with PHP. Using the ORDER BY rand() method in your query maybe a bit slow so here&#039;s what we&#039;ve got.</p>
<h3>The Premade Method</h3>
<p>As I mentioned in the beginning. MySQL actually has a premade method that allows you to select a random row by querying for it.</p>
<div class="dean_ch" style="white-space: wrap;"><a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span><span class="st0">&quot;SELECT * FROM table ORDER BY rand() LIMIT 0,1&quot;</span><span class="br0">&#41;</span>;</div>
<p>This method is great but if you are randomizing from a large table this method is not very efficient and with a high volume of queries it really puts a strain on your DB.</p>
<h3>The New Method &#8211; Randomizing 1 Entry</h3>
<p>Now here&#039;s the new solution by selecting a random row with the offset. This method will query exactly to the random entry; thus, making this process much faster.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="re0">$query</span> = <span class="st0">&quot;SELECT * FROM table&quot;</span><br />
<span class="re0">$num_rows</span> = <a href="http://www.php.net/mysql_num_rows"><span class="kw3">mysql_num_rows</span></a><span class="br0">&#40;</span> <a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span> <span class="re0">$query</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span>;</p>
<p><span class="co1">//Random Number</span><br />
<span class="re0">$rand_row</span> = <a href="http://www.php.net/rand"><span class="kw3">rand</span></a><span class="br0">&#40;</span><span class="nu0">0</span>, <span class="re0">$num_rows</span> &#8211; <span class="nu0">1</span><span class="br0">&#41;</span>;</p>
<p><span class="co1">//Selecting the Random Row</span><br />
<span class="re0">$rand_row</span> = <span class="re0">$query</span> .= <span class="st0">&quot; LIMIT $rand_row, 1&quot;</span>;</div>
<p>Now you can modify the $query line to include your filters and sorting stuff, whatever you need.</p>
<h3>The New Method &#8211; Randomizing More than 1 Entry</h3>
<p>The above method right now can only select a single row. What if you want to randomized more than one item. Here&#039;s a method to select multiple items without overlap.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="co1">//Set the Amount of Random Rows you want</span><br />
<span class="re0">$rand_amount</span> = <span class="nu0">5</span>;</p>
<p><span class="re0">$query</span> = <span class="st0">&quot;SELECT * FROM table&quot;</span><br />
<span class="re0">$num_rows</span> = <a href="http://www.php.net/mysql_num_rows"><span class="kw3">mysql_num_rows</span></a><span class="br0">&#40;</span> <a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span> <span class="re0">$query</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span>;</p>
<p><span class="co1">//Make sure you have more than the amount of random count you want</span><br />
<span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$num_rows</span> &lt;= <span class="re0">$rand_amount</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//An Array of Numbers Choosen</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$rand_array</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Do IT!!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span><span class="re0">$i</span> = <span class="nu0">1</span>; <span class="re0">$i</span> &gt;= <span class="re0">$rand_amount</span>; <span class="re0">$i</span>++ <span class="br0">&#41;</span> <span class="br0">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">do</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$rand_added</span> = <span class="kw2">FALSE</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$rand_num</span> = <a href="http://www.php.net/rand"><span class="kw3">rand</span></a><span class="br0">&#40;</span><span class="nu0">0</span>, <span class="re0">$num_rows</span> &#8211; <span class="nu0">1</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Should We Add to Array</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!<a href="http://www.php.net/in_array"><span class="kw3">in_array</span></a><span class="br0">&#40;</span><span class="re0">$rand_num</span>, <span class="re0">$rand_array</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$rand_array</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="re0">$rand_num</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$rand_added</span> = <span class="kw2">TRUE</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">while</span> <span class="br0">&#40;</span>!<span class="re0">$rand_added</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//You now have a set of random row numbers to choose from</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Display The Result of What you have</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">foreach</span> <span class="br0">&#40;</span><span class="re0">$rand_array</span> <span class="kw1">as</span> <span class="re0">$rand_num</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_query</span> = <span class="re0">$query</span> . <span class="st0">&quot; LIMIT $rand_num, 1&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Displaying What you have</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$current_row</span> = <a href="http://www.php.net/mysql_fetch_array"><span class="kw3">mysql_fetch_array</span></a><span class="br0">&#40;</span> <a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span> <span class="re0">$new_query</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>To be honest you can use the multiple randomizer and just set the random amount to 1 but the other method saves you like 20 lines of code. Imagine how much faster that could be!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.crankberryblog.com/2010/random-row-from-your-mysql-table-with-php-alternative-to-order-by-rand/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

