<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.3.3" -->
<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/"
	>

<channel>
	<title>edrackham &#187; MySQL</title>
	<link>http://edrackham.com</link>
	<description>PHP, MySQL, and Other Web Snippets!</description>
	<pubDate>Wed, 25 Jun 2008 13:38:14 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.3</generator>
	<language>en</language>
			<item>
		<title>Get Random Row with MySQL Without ORDER BY RAND()</title>
		<link>http://edrackham.com/featured/get-random-row-with-mysql-without-order-by-rand/</link>
		<comments>http://edrackham.com/featured/get-random-row-with-mysql-without-order-by-rand/#comments</comments>
		<pubDate>Wed, 05 Mar 2008 12:12:24 +0000</pubDate>
		<dc:creator>Ed</dc:creator>
		
		<category><![CDATA[Featured]]></category>

		<category><![CDATA[MySQL]]></category>

		<category><![CDATA[fast]]></category>

		<category><![CDATA[random row]]></category>
<category>fast</category><category>MySQL</category><category>random row</category>
		<guid isPermaLink="false">http://edrackham.com/mysql/get-random-row-with-mysql-without-rand/</guid>
		<description><![CDATA[This is an update to a previous post of mine which uses the RAND() method. Using the following code, you can retrieve a random row much, much faster (MySQL 4.1.x/5.0.x), with thanks to Jan Kneschke:

SELECT &#60;COLUMN&#62; FROM &#60;TABLE&#62; AS r1
JOIN &#40;SELECT ROUND&#40;
&#160; RAND&#40; &#41; * &#40;
&#160; &#160; SELECT MAX&#40; id &#41; FROM &#60;TABLE&#62;&#41;
&#160; &#41; AS [...]]]></description>
			<content:encoded><![CDATA[<p>This is an update to a <a href="http://edrackham.com/php/get-random-row-with-mysql/" title="Slower, but useful for smaller tables">previous post of mine</a> which uses the RAND() method. Using the following code, you can retrieve a random row much, much faster (MySQL 4.1.x/5.0.x), with thanks to Jan Kneschke:</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="kw1">SELECT</span> &lt;COLUMN&gt; <span class="kw1">FROM</span> &lt;TABLE&gt; <span class="kw1">AS</span> r1<br />
<span class="kw1">JOIN</span> <span class="br0">&#40;</span><span class="kw1">SELECT</span> ROUND<span class="br0">&#40;</span><br />
&nbsp; RAND<span class="br0">&#40;</span> <span class="br0">&#41;</span> * <span class="br0">&#40;</span><br />
&nbsp; &nbsp; <span class="kw1">SELECT</span> MAX<span class="br0">&#40;</span> id <span class="br0">&#41;</span> <span class="kw1">FROM</span> &lt;TABLE&gt;<span class="br0">&#41;</span><br />
&nbsp; <span class="br0">&#41;</span> <span class="kw1">AS</span> id<br />
<span class="br0">&#41;</span> <span class="kw1">AS</span> r2<br />
<span class="kw1">WHERE</span> r1.id &gt;= r2.id<br />
<span class="kw1">ORDER</span> <span class="kw1">BY</span> r1.id <span class="kw1">ASC</span><br />
<span class="kw1">LIMIT</span> <span class="nu0">1</span>;</div>
</div>
<p>
<div align="center"><script type="text/javascript"><!--
google_ad_client = "pub-4159646232668987";
/* 468x60, created 3/10/08 */
google_ad_slot = "7234899452";
google_ad_width = 468;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
</p>
<p>Replace:</p>
<ul>
<li><strong>&lt;COLUMN&gt;</strong> with the name of the column(s) you wish to retrieve</li>
<li><strong>&lt;TABLE&gt;</strong> with the name of the table you wish to retrieve the data from</li>
</ul>
<p>I&#039;ve tested this with a table with over 660,000 records, and got a response in 0.0200 seconds, whereas with ORDER BY RAND() i got a response in 2.1599 seconds.</p>
<h4>Total Number of Rows:</h4>
<p><img src="http://edrackham.com/images/posts/num-rows.gif" alt="Cardinality" style="border:1px solid #ccc" /></p>
<h4>Old Method:</h4>
<p><img src="http://edrackham.com/images/posts/faster-rand-mysql-old-query.gif" alt="Old Method" style="border:1px solid #ccc" /></p>
<h4>New Method:</h4>
<p><img src="http://edrackham.com/images/posts/faster-rand-mysql-new-query.gif" alt="New Method" style="border:1px solid #ccc" /></p>
<p>
<div align="center"><script type="text/javascript"><!--
google_ad_client = "pub-4159646232668987";
/* 468x60, created 3/10/08 */
google_ad_slot = "7234899452";
google_ad_width = 468;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
</p>
]]></content:encoded>
			<wfw:commentRss>http://edrackham.com/featured/get-random-row-with-mysql-without-order-by-rand/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Get Random Row with MySQL</title>
		<link>http://edrackham.com/php/get-random-row-with-mysql/</link>
		<comments>http://edrackham.com/php/get-random-row-with-mysql/#comments</comments>
		<pubDate>Sat, 09 Feb 2008 00:51:31 +0000</pubDate>
		<dc:creator>Ed</dc:creator>
		
		<category><![CDATA[MySQL]]></category>

		<category><![CDATA[PHP]]></category>
<category>MySQL</category><category>PHP</category><category>Tutorial</category>
		<guid isPermaLink="false">http://edrackham.com/php/get-random-row-with-mysql/</guid>
		<description><![CDATA[UPDATE: Please see my newer atricle on how to retrieve a random row, faster, without RAND().






This post assumes you know how to create and use a connection to a MySQL database in PHP and have a table named &#039;quotes&#039; as shown below. In this post, I will aim to teach you how to use PHP [...]]]></description>
			<content:encoded><![CDATA[<p><strong>UPDATE:</strong> Please see my newer atricle on <a href="http://edrackham.com/mysql/get-random-row-with-mysql-without-rand/" title="Blink, and you'll miss it!">how to retrieve a random row, faster, without RAND()</a>.</p>
<p>
<div align="center"><script type="text/javascript"><!--
google_ad_client = "pub-4159646232668987";
/* 468x60, created 3/10/08 */
google_ad_slot = "7234899452";
google_ad_width = 468;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
</p>
<p>This post assumes you know how to create and use a connection to a MySQL database in PHP and have a table named &#039;quotes&#039; as shown below. In this post, I will aim to teach you how to use PHP to pull random quotes from a MYSQL table of quotes. This can be easily extended to pull a random banner as will be explained at the end of the post.</p>
<p>Let&#039;s firstly assume we have a MySQL table similar to the following:</p>
<pre>+----+------------------------------------------------+
| id | quote                                          |
+----+------------------------------------------------+
| 1  | I know Karate... and many other Chinese words! |
+----+------------------------------------------------+
| 2  | w00t this is geeky                             |
+----+------------------------------------------------+
| 3  | You're CLINICALLY MENTAL!                      |
+----+------------------------------------------------+</pre>
<p>
<div align="center"><script type="text/javascript"><!--
google_ad_client = "pub-4159646232668987";
/* 468x60, created 3/10/08 */
google_ad_slot = "7234899452";
google_ad_width = 468;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
</p>
<p>Now, in your PHP code, we need to:</p>
<ol>
<li>Build a suitable MySQL query to obtain a random result from the &#039;quotes&#039; table.</li>
<li>Store the result of the query to be used in the HTML somewhere.</li>
<li>Output the result in the HTML somewhere.</li>
</ol>
<p>So, for step one we&#039;d use something like the following:</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="re0">$sSQLQuery</span> = <span class="st0">&#034;SELECT quote FROM quotes ORDER BY RAND() LIMIT 1&#034;</span>;<br />
<span class="re0">$aResult</span> = <a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span><span class="re0">$sSQLQuery</span><span class="br0">&#41;</span>;<br />
<span class="re0">$aRow</span> = <a href="http://www.php.net/mysql_fetch_array"><span class="kw3">mysql_fetch_array</span></a><span class="br0">&#40;</span><span class="re0">$aResult</span>, MYSQL_ASSOC<span class="br0">&#41;</span>;<br />
<span class="re0">$sQuoteOfTheDay</span> = <span class="re0">$aRow</span><span class="br0">&#91;</span><span class="st0">&#039;quote&#039;</span><span class="br0">&#93;</span>;</div>
</div>
<p>There, the variable &#039;$sQuoteOfTheDay&#039; now has the value of our randomly pulled quote. Let&#039;s just analyse each line of the code above to see what it does.</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="re0">$sSQLQuery</span> = <span class="st0">&#034;SELECT quote FROM quotes ORDER BY RAND() LIMIT 1&#034;</span>;</div>
</div>
<p>This line stores the MySQL query we&#039;re going to use against the database. It says, in laymans terms, &#034;Select just one random value of the quote field from the table named quotes&#034;. All this line does though is store the query into the variable &#039;$sSQLQuery&#039;.</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="re0">$aResult</span> = <a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span><span class="re0">$sSQLQuery</span><span class="br0">&#41;</span>;</div>
</div>
<p>This line runs the MySQL query, storing the result of running the query in the variable &#039;$aResult&#039;. It&#039;s important that we store the result of the mysql_query in a variable, as the result of running a successful MySQL query using PHP&#039;s function &#039;mysql_query&#039; doesn’t return a nicely formatted array that we can necessarily use.</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="re0">$aRow</span> = <a href="http://www.php.net/mysql_fetch_assoc"><span class="kw3">mysql_fetch_assoc</span></a><span class="br0">&#40;</span><span class="re0">$aResult</span><span class="br0">&#41;</span>;</div>
</div>
<p>This is probably the hardest line for me to explain. Firstly, many of you may have seen a similar line like this used in a &#039;while&#039; loop. However, our MySQL query used the &#039;LIMIT 1&#039; string, so we know we’re only going to get ONE result, hence no need for a loop. The function &#039;mysql_fetch_assoc&#039; takes one parameter: the result of the successful &#039;mysql_query&#039; which as we know is &#039;$aResult&#039;. My biggest tip here is to use the &#039;assoc&#039; method wherever possible, as it creates the array in such a way that we can reference each element by the column name, not a number. This is particularly useful if you ever update the MySQL table to have more columns.</p>
<p>Anyway, this line basically says &#039;Fill the variable &#039;$aRow&#039; with the CURRENT row of the returned query&#039;. We know that the CURRENT row of the returned query is the ONLY row, hence (again) the lack of a loop. As the result of our query would return something like:</p>
<pre>+-----------------------------------------------+
| quote                                         |
+-----------------------------------------------+
| You're CLINICALLY MENTAL!                     |
+-----------------------------------------------+</pre>
<p>
<div align="center"><script type="text/javascript"><!--
google_ad_client = "pub-4159646232668987";
/* 468x60, created 3/10/08 */
google_ad_slot = "7234899452";
google_ad_width = 468;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
</p>
<p>Our variable (or array) would literally look something like:</p>
<div class="codesnip-container" >
<div class="codesnip"><a href="http://www.php.net/array"><span class="kw3">Array</span></a> <span class="br0">&#40;</span> <span class="st0">&#034;quote&#034;</span> = <span class="st0">&#034;You&#039;re CLINICALLY MENTAL!&#034;</span> <span class="br0">&#41;</span></div>
</div>
<p>Which leads us on to our last line:</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="re0">$sQuoteOfTheDay</span> = <span class="re0">$aRow</span><span class="br0">&#91;</span><span class="st0">&#039;quote&#039;</span><span class="br0">&#93;</span>;</div>
</div>
<p>Which just assigns the value of &#039;$aRow[&#039;quote&#039;]&#039; to the variable &#039;$sQuoteOfTheDay&#039;. In other words, &#039;$sQuoteOfTheDay&#039; now has the value of a random quote pulled from the database of quotes.</p>
<p>To use this in an HTML page, we would simply just use this (AFTER the above code has grabbed the random quote from the DB for us):</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="kw2">&lt;?</span>= <span class="re0">$sQuoteOfTheDay</span>; <span class="kw2">?&gt;</span></div>
</div>
<p>Which will output the quote of the day somewhere in the HTML code.</p>
<p>As I said at the beginning, this can be extended easily to pull an image for a banner by simply changing the quotes table to store image paths such as &#039;images/my_image.png&#039; which can then be pulled in the same way, and then output similar to the following:</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;img src=<span class="st0">&#034;&lt;?= $sImageOfTheDay; ?&gt;&#034;</span> alt=<span class="st0">&#034;My Image&#034;</span> /&gt;</div>
</div>
<p>Obviously we changed the variable name here to $sImageOfTheDay just to keep things constant.</p>
<p>Hope this has helped someone!</p>
]]></content:encoded>
			<wfw:commentRss>http://edrackham.com/php/get-random-row-with-mysql/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
