<?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>A-Connect &#187; PHP</title>
	<atom:link href="http://a-connect.co.uk/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://a-connect.co.uk</link>
	<description>Cornwall Web Design</description>
	<lastBuildDate>Thu, 14 Jan 2010 11:11:37 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Caching remote MySQL queries with PHP</title>
		<link>http://a-connect.co.uk/php/caching-remote-mysql-queries-with-php/</link>
		<comments>http://a-connect.co.uk/php/caching-remote-mysql-queries-with-php/#comments</comments>
		<pubDate>Sat, 20 Jun 2009 16:06:37 +0000</pubDate>
		<dc:creator>a_connect</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[caching mysql]]></category>
		<category><![CDATA[class_db]]></category>
		<category><![CDATA[remote mysql]]></category>

		<guid isPermaLink="false">http://a-connect.co.uk/?p=30</guid>
		<description><![CDATA[The problem:
What I wanted to do here was find a way of speeding up a query that was run on MySQL database on a different server. Also I&#8217;m a big fan of caching generally, especially when the data is only updated once or twice a day.
When I was running the queries directly off the remote [...]]]></description>
			<content:encoded><![CDATA[<p><strong>The problem:</strong></p>
<p>What I wanted to do here was find a way of speeding up a query that was run on MySQL database on a different server. Also I&#8217;m a big fan of caching generally, especially when the data is only updated once or twice a day.</p>
<p>When I was running the queries directly off the remote server it was noticably slow but with this solution in place things are flying!</p>
<p>Whilst there are ways of caching MySQL queries using MySQL this would be pretty pointless for a remote query, the only solution was to cache the results locally.</p>
<p><strong>The solution:</strong></p>
<p><a href="http://www.troywolf.com/articles/php/class_db/" target="_blank">class_db.php</a> by Troy Wolf</p>
<p>What this class does is provide an layer between your query and database server (it supports several DBs) which runs the query and then writes the results to a file in a serialized format. E.g.</p>
<blockquote><p>a:10:{i:0;a:8:{s:13:&#8221;Dunster House&#8221;;s:10:&#8221;short_desc&#8221;;s:157:&#8221;A warm welcome to Dunster House. We are a friendly family run Guest House situated within walking distance of Torquay&#8217;s main attractions. Beds from &amp;pound;20&#8243;;s:4:&#8221;town&#8221;;s:7:&#8221;Torquay&#8221;;s:3:&#8221;lat&#8221;;N;s:3:&#8221;lon&#8221;;N;}&#8230;.</p></blockquote>
<p>In the class you need to hard code the details of your DB connection(s) and set up your cache directory. Then it&#8217;s as straightforward as:</p>
<pre>include("class_db.php");

$d = new db(0); //use first connection info from class_db

$sql = "SELECT `id`
 FROM accommodation
 WHERE `type` = ".$catNo;
$q_name = 'num_'.md5($catNo;);
$rows = $d-&gt;fetch($sql, 3600, $q_name);//run query, cache for 1hr, save cahce file as $q_name

foreach($rows as $row){
...
}</pre>
<p>Easy as that. You have now run the query on the remote server and created a cached local result which will be called until it expires.</p>
<p>For an example of the script in action: <a href="http://www.devon-stay.com/Ilfracombe/camping.htm" target="_blank">http://www.devon-stay.com/Ilfracombe/camping.htm</a></p>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 27px; width: 1px; height: 1px;">http://www.devon-stay.com/Ilfracombe/camping.htm</div>
]]></content:encoded>
			<wfw:commentRss>http://a-connect.co.uk/php/caching-remote-mysql-queries-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 0.211 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2010-05-24 08:59:56 -->
<!-- Compression = gzip -->