<?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>Brian Bell</title>
	<atom:link href="http://webguru.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://webguru.org</link>
	<description>Bits of web-dev, travel and personal musings...</description>
	<lastBuildDate>Mon, 09 Nov 2009 21:36:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to use Google Charts API in your Secure, HTTPS webpage</title>
		<link>http://webguru.org/2009/11/09/php/how-to-use-google-charts-api-in-your-secure-https-webpage/</link>
		<comments>http://webguru.org/2009/11/09/php/how-to-use-google-charts-api-in-your-secure-https-webpage/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 21:36:16 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WebDev]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[google charts]]></category>
		<category><![CDATA[https]]></category>

		<guid isPermaLink="false">http://webguru.org/?p=42</guid>
		<description><![CDATA[So you love Google Charts API and want to spice up your app with lots of pretty bar charts, Venn diagrams, Google-o-meters and 2D bar codes (QR Codes) &#8211; only problem is, you require that users connect to your site using a secure connection (https://yoursite.com/whatever).  Well, as you are probably aware, the Google Charts [...]]]></description>
			<content:encoded><![CDATA[<p>So you love Google Charts API and want to spice up your app with lots of pretty bar charts, Venn diagrams, Google-o-meters and 2D bar codes (QR Codes) &#8211; only problem is, you require that users connect to your site using a secure connection (<strong>https</strong>://yoursite.com/whatever).  Well, as you are probably aware, the Google Charts API doesn&#8217;t ex</p>
<p>actly support SSL connections&#8230; directly.</p>
<p>Have no fear! If you are willing to give up a little bit of your own bandwidth, you can work around this problem.  We are going to do this using a simple PHP script and if you want to get really fancy, Memcached to keep the redundant API calls to a minimum.</p>
<p>Let&#8217;s get started.<br />
<span id="more-42"></span><br />
First, let&#8217;s make sure we understand why it is a problem to include a non-secure Google Charts API into your otherwise secure page.  When your users are browsing to your page using an address starting with <strong>https://</strong>, their browser is going to negotiate an TLS/SSL connection which will encrypt all the data flowing between your client and the server.  If any element on the page (image, external CSS or JavaScript, etc.) is not being called from a secured connection as well, then the browser will throw up a warning.  Each browser is different, but most will alert the user with a broken lock and/or a popup message.  The user can usually accept this warning and proceed at their own risk, but this is not acceptable for a production website or application.</p>
<p>The obvious answer to this problem is to make sure that all external elements on the page are loaded over a secure connection.  So &#8211; how do we do that with Google Charts? Google does not allow you to just take a traditional API call and slap a &#8220;https://&#8221; in front of it in lieu of the expected http://.  Your request will be <strong>denied</strong>.</p>
<h2>Basic Implementation</h2>
<p>Here&#8217;s what we do:</p>
<ol>
<li>Create a simple PHP script that will fetch a chart from Google Charts API</li>
<li>Change your web page to call the new PHP &#8220;fetcher&#8221; instead of Google Charts directly.</li>
<li>Make sure the PHP script is being called by your page over your https:// connection</li>
</ol>
<p>It is pretty much that simple.</p>
<p>Here is the before example:</p>
<pre class="brush: xml;">
&lt;!-- HTML snippet --&gt;
&lt;img src='http://chart.apis.google.com/chart?cht=p&amp;chd=s:Uf9a&amp;chs=200x100&amp;chl=January|February|March|April' alt='Pie Chart'/&gt;
</pre>
<p>Here is the after example:</p>
<pre class="brush: xml;">
&lt;!-- HTML snippet --&gt;
&lt;img src='https://mywebsite.com/gchart.php?api_url=http%3A%2F%2Fchart.apis.google.com%2Fchart%3Fcht%3Dp%26chd%3Ds%3AUf9a%26chs%3D200x100%26chl%3DJanuary%7CFebruary%7CMarch%7CApril' alt='Pie Chart'/&gt;
</pre>
<p>Notice, the same API URI is used in the &#8220;after&#8221; example, <strong>but it is URL Encoded</strong>.  This just makes it safer to pass along to the PHP script.</p>
<p>Here is the PHP script that makes this work:</p>
<pre class="brush: php;">
&lt;?php

$url = urldecode($_GET['api_url']);

$image_contents = file_get_contents($url);
echo $image_contents;
exit;
</pre>
<p>A quick note about this: you may be saying &#8211; where is your closing PHP tag!?!  Well, PHP doesn&#8217;t require that you have a closing PHP tag, as the PHP interpreter will automatically close any open tags.  I do this on just about every file that will be used as an include of some sorts &#8211; particularly in something like this where any extra, inadvertent white space at the end of the file can alter the output adversely and cause errors in the page.</p>
<h2>Advanced Implementation w/ Caching</h2>
<p>The script as it is above, is fine and dandy, but terribly inefficient.  You see, every time the user requests the page, your script is going out and using <strong>your bandwidth</strong> to make the request to the Google Charts API instead of the client.  You are becoming the proxy for all requests to and from Google Charts.  This will not fly &#8211; especially if you are in a high volume environment.</p>
<p>So what is the solution? I chose Memcache to store a copy of the image contents for a certain period of time before having to re-query the Charts API and re-fetch the data again.</p>
<p>It looks something like this:</p>
<pre class="brush: php;">
&lt;?php

$url = urldecode($_GET['api_url']);
$cache_key = md5($url);
$item_cache_expire = 3600;
$mc_host = '127.0.0.1';
$mc_port = 11211;

# Connect to Memcache
$memcache = new Memcache;
$memcache-&gt;connect($mc_host, $mc_port) or die (&quot;Could not connect&quot;);

if ( $get_result = $memcache-&gt;get($cache_key) ) {
    $image_contents = $get_result-&gt;chart_image;
}
else {
    $image_contents = file_get_contents($url);

    $tmp_object = new stdClass;
    $tmp_object-&gt;chart_image = $image_contents;

    $memcache-&gt;set($cache_key, $tmp_object, false, $item_cache_expire) or die (&quot;Failed to save data at the server&quot;);

}
echo $image_contents;
exit;
</pre>
<p>Here&#8217;s what is happening. </p>
<pre class="brush: php;">
&lt;?php

$url = urldecode($_GET['api_url']);
$cache_key = md5($url);
$item_cache_expire = 3600;
$mc_host = '127.0.0.1';
$mc_port = 11211;
</pre>
<p>Setup some variables we&#8217;ll need in the script.  Grab the api_url value from the GET array, create an MD5 hash of the request URL (we&#8217;ll use this as our memcache item key), set the expiration time of the cached item to 3600 seconds (a.k.a. 1 hour) and finally provide the connection information to your memecached server. </p>
<pre class="brush: php;">
# Connect to Memcache
$memcache = new Memcache;
$memcache-&gt;connect($mc_host, $mc_port) or die (&quot;Could not connect&quot;);
</pre>
<p>Instantiate a new Memcache object using the connection info provided above.</p>
<pre class="brush: php;">
if ( $get_result = $memcache-&gt;get($cache_key) ) {
    $image_contents = $get_result-&gt;chart_image;
}
else {
    $image_contents = file_get_contents($url);

    $tmp_object = new stdClass;
    $tmp_object-&gt;chart_image = $image_contents;

    $memcache-&gt;set($cache_key, $tmp_object, false, $item_cache_expire) or die (&quot;Failed to save data at the server&quot;);
}
</pre>
<p>Using the MD5 hash that we created above, check to see if there is a valid cached object on the memcache server for us to retrieve.  If not, go out and grab a fresh version from Google Charts API and stick it into memcache.  Either way, we end up with a variable $image_contents which contains the desired chart image.</p>
<pre class="brush: php;">
echo $image_contents;
exit;
</pre>
<p>All that is left to do now is print out the image contents and exit the script.  Again, I do not use a closing PHP tag on purpose to avoid extra whitespace.</p>
<h2>All ye, all ye, now hear this&#8230;</h2>
<p>This is a very basic example.  It should probably be categorized more as a proof of concept. It does NOT, I repeat &#8211; DOES NOT &#8211; take into account a variety of security concerns such as SQL injection or CSRF (cross-site request forgeries), etc. Notice that I didn&#8217;t even validate the user input from the GET variable.  Who does that!?!  So, in other words, don&#8217;t just copy and paste and then come back and blame me for getting your site hacked.  Be smart about employing something like this.</p>
<p>BTW, if you want to know more about how to make your PHP scripts more secure, go see Chris Shifflet&#8217;s excellent blog at <a href="http://shiflett.org/" target="_blank">http://shiflett.org/</a>.  </p>
]]></content:encoded>
			<wfw:commentRss>http://webguru.org/2009/11/09/php/how-to-use-google-charts-api-in-your-secure-https-webpage/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP Object Get/Set Generator</title>
		<link>http://webguru.org/2009/04/17/php/php-object-getset-generator/</link>
		<comments>http://webguru.org/2009/04/17/php/php-object-getset-generator/#comments</comments>
		<pubDate>Fri, 17 Apr 2009 21:49:23 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://webguru.org/?p=39</guid>
		<description><![CDATA[Well, I know I haven&#8217;t created anything new here &#8211; but as Frank said, &#8220;I did it my way!&#8221;
I recently found myself creating more and more objects to store data within apps. I&#8217;m not going to get into all the ins and outs of why this is good practice, but it lead me to whipping [...]]]></description>
			<content:encoded><![CDATA[<p>Well, I know I haven&#8217;t created anything new here &#8211; but as Frank said, &#8220;I did it my way!&#8221;</p>
<p>I recently found myself creating more and more objects to store data within apps. I&#8217;m not going to get into all the ins and outs of why this is good practice, but it lead me to whipping up this simple script. I had seen a few other similar tools on the inter-webs but none of them worked quite like I needed. </p>
<p>If you&#8217;e not familiar with the OO approach for storing data within a script (is there a better way to describe this?) here the gist. </p>
<p>Say you are storing user profile information in your favorite RDBMS. You have a user profile page and will pull data for a specific profile to use in that page. Well, instead of just doing a fetch and grabbing data &#8211; creating a bunch of variables, etc., you can take the OO approach. Load all those vars into an object, with nice features like getters and setters &#8211; which may manipulate the data, etc. Then you have a clean interface to the data. </p>
<p>My IDE doesn&#8217;t have any wizards for this stuff (I think Eclipse can &#8211; or has plugins that can) and it can be a bit time consuming to do. Also &#8211; its very repetitive to get your basic class structure together. SO &#8211; why not write a little script to do this! Like I said, I have seen some others out there that were apparently written for PHP4 or just didn&#8217;t format the methods like I prefer. </p>
<p>Check it out and let me know what you think.<br /> <a href="http://webguru.org/getset">http://webguru.org/getset</a></p>
<p>The resulting class allows you to do stuff like this:</p>
<pre class="brush: php;">
&lt;?php

// create an array of basic user data
$data = array();
$data[&quot;user_id&quot;] = 3;
$data[&quot;name&quot;] = &quot;Brian Bell&quot;;
$data[&quot;email&quot;] = &quot;brian@example.com&quot;;

$user = new UserDataObj( $data );
echo &quot;User {$user-&gt;get_name()} has an id of {$user-&gt;get_id()} and an email address of {$user-&gt;get_email()}. &quot;;

/*
  You can also instantiate the object with no argument,
  and set things manually
*/
$user = new UserDataObj();
$user-&gt;set_name(&quot;Brian Bell&quot;);
$user-&gt;set_email(&quot;brian@example.com&quot;);

// also comes in handy to pass the data object to your CrUD methods
$new = new Users();
/*
  pass in the data object from above so
  the create_user() method has everything it needs.
*/
$new-&gt;create_user( $user );

?&gt;
</pre>
<p>Let me know if you have any comments or suggestions. I don&#8217;t have a lot of time to contribute to it &#8211; but I can easily see how it could be extended to give the user some options on formatting, etc. </p>
]]></content:encoded>
			<wfw:commentRss>http://webguru.org/2009/04/17/php/php-object-getset-generator/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ReadTheStimulus.org</title>
		<link>http://webguru.org/2009/01/28/general/readthestimulusorg/</link>
		<comments>http://webguru.org/2009/01/28/general/readthestimulusorg/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 00:56:58 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://webguru.org/2009/01/28/general/readthestimulusorg/</guid>
		<description><![CDATA[&#8220;$850 Billion, 1588 pages, and counting&#8230; somebody needs to read it!&#8221;
This site has popped up with an easy way to search and drill-down into the entire text of the House Stimulus bill.  Just take a few minutes and browse through this pork-packet.  Unbelievable!!
]]></description>
			<content:encoded><![CDATA[<p>&#8220;$850 Billion, 1588 pages, and counting&#8230; <em>somebody</em> needs to read it!&#8221;</p>
<p>This site has popped up with an easy way to search and drill-down into the entire text of the House Stimulus bill.  Just take a few minutes and browse through this pork-packet.  Unbelievable!!</p>
]]></content:encoded>
			<wfw:commentRss>http://webguru.org/2009/01/28/general/readthestimulusorg/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Memory-tight, multi-threaded PHP Daemon</title>
		<link>http://webguru.org/2008/11/17/php/memory-tight-multi-threaded-php-daemon/</link>
		<comments>http://webguru.org/2008/11/17/php/memory-tight-multi-threaded-php-daemon/#comments</comments>
		<pubDate>Mon, 17 Nov 2008 15:49:02 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WebDev]]></category>

		<guid isPermaLink="false">http://webguru.org/2008/11/17/php/memory-tight-multi-threaded-php-daemon/</guid>
		<description><![CDATA[Came across this nifty project on Google Code.  It&#8217;s a multi-threaded, object-oriented PHP daemon which is very nicely written and easy to use.
http://code.google.com/p/phpmultithreadeddaemon/
Here&#8217;s their example of how to implement it.  Everything needed to run the daemon is in the single included file &#8220;class.MTDaemon.php&#8221; &#8211; cool!


&#60;?php

error_reporting(E_ALL);

require_once(include/class.MTDaemon.php);

class MTTest extends MTDaemon {

public function getNext($slot)
{
$this-&#62;lock();
$num = $this-&#62;getVar(&#34;num&#34;);
if [...]]]></description>
			<content:encoded><![CDATA[<p>Came across this nifty project on Google Code.  It&#8217;s a multi-threaded, object-oriented PHP daemon which is very nicely written and easy to use.</p>
<p><a href="http://code.google.com/p/phpmultithreadeddaemon/" title="PHP Daemon Project on Google Code">http://code.google.com/p/phpmultithreadeddaemon/</a></p>
<p>Here&#8217;s their example of how to implement it.  Everything needed to run the daemon is in the single included file &#8220;class.MTDaemon.php&#8221; &#8211; cool!</p>
<pre class="brush: php;">

&lt;?php

error_reporting(E_ALL);

require_once(include/class.MTDaemon.php);

class MTTest extends MTDaemon {

public function getNext($slot)
{
$this-&gt;lock();
$num = $this-&gt;getVar(&quot;num&quot;);
if ($num == null) $num = 1;
if ($num &gt; 100) {
$this-&gt;unlock();
return null;
}
$this-&gt;unlock();

$rand = rand(0, 5);
echo &quot;Next for slot &quot; . $slot . &quot; : &quot; . $rand . &quot;\n&quot;;
if ($rand == 0) return null;
else return $rand;
}

public function run($next, $slot)
{
$rand = rand(3, 10);
$this-&gt;lock();
$num = $this-&gt;getVar(&quot;num&quot;);
$this-&gt;setVar(&quot;num&quot;, $this-&gt;getVar(&quot;num&quot;) + 1);
$this-&gt;unlock();
echo &quot;## Iteration #&quot; . number_format($num) . &quot; in &quot; . $rand . &quot;sec&quot; . &quot;\n&quot;;

sleep($rand);
return 0;
}

}

$mttest = new MTTest(2);
$mttest-&gt;handle();

?&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://webguru.org/2008/11/17/php/memory-tight-multi-threaded-php-daemon/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extremely helpful Microsoft Silverlight warning</title>
		<link>http://webguru.org/2008/08/14/general/extremely-helpful-microsoft-silverlight-warning/</link>
		<comments>http://webguru.org/2008/08/14/general/extremely-helpful-microsoft-silverlight-warning/#comments</comments>
		<pubDate>Thu, 14 Aug 2008 15:31:55 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[eeediots]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://webguru.org/2008/08/14/general/extremely-helpful-microsoft-silverlight-warning/</guid>
		<description><![CDATA[I had an old version of MS Silverlight installed on my machine. This is Microsoft&#8217;s attempt to compete with Adobe&#8217;s (formerly Macromedia&#8217;s) Flash.  Way to go guys &#8211; that train left the station in, oh, I don&#8217;t know&#8230; 1996?
Bottom line is that nobody is really using Silverlight &#8211; besides MS&#8217;s attempts to force it down [...]]]></description>
			<content:encoded><![CDATA[<p>I had an old version of MS Silverlight installed on my machine. This is Microsoft&#8217;s attempt to compete with Adobe&#8217;s (formerly Macromedia&#8217;s) Flash.  Way to go guys &#8211; that train left the station in, oh, I don&#8217;t know&#8230; 1996?</p>
<p>Bottom line is that nobody is really using Silverlight &#8211; besides MS&#8217;s attempts to force it down your throat on their own website and through some strategic marketing moves (ie. with NBC Olympics online coverage).  Plus, the sites that do offer streaming media via Silverlight were thankfully not stupid enough to make that your only option.  I haven&#8217;t been to a site yet that doesn&#8217;t offer me the same feature with Flash Video for example.</p>
<p>Anyway &#8211; there is a newer version of Silverlight out -  so when I visit a page that is trying to stream content via the Silverlight plugin, I get this error:</p>
<div align="center"><img src="http://content.screencast.com/users/brianfd/folders/Jing/media/1c21e6a8-385c-4a67-8e55-b5424a81cba1/2008-08-14_1123.png" border="0" width="518" height="119" /></div>
<p>How awesome is that?  Did MS really think I could click the link and get to the update page? No &#8211; maybe I was expected remember that URL or <strong>write it down</strong> so that I could quickly download the newer version and get to my content.</p>
<p>How classic is that?!?!</p>
]]></content:encoded>
			<wfw:commentRss>http://webguru.org/2008/08/14/general/extremely-helpful-microsoft-silverlight-warning/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why doesn&#8217;t Lindsey Graham just go ahead and claim the (D)?</title>
		<link>http://webguru.org/2008/08/14/politics/why-doesnt-lindsey-graham-just-go-ahead-and-claim-the-d/</link>
		<comments>http://webguru.org/2008/08/14/politics/why-doesnt-lindsey-graham-just-go-ahead-and-claim-the-d/#comments</comments>
		<pubDate>Thu, 14 Aug 2008 12:54:38 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[Politics]]></category>

		<guid isPermaLink="false">http://webguru.org/2008/08/14/politics/why-doesnt-lindsey-graham-just-go-ahead-and-claim-the-d/</guid>
		<description><![CDATA[What a waste of a senate seat.  The goofy, nil-brained senator from South Carolina has once again (shock! surprise!) swerved to the left in an effort to advance his personal interests.
And so, last Friday, in stumbled Sens. Lindsey Graham, John Thune, Saxby Chambliss, Bob Corker and Johnny Isakson &#8212; alongside five Senate Democrats. This &#8220;Gang [...]]]></description>
			<content:encoded><![CDATA[<p>What a waste of a senate seat.  The goofy, nil-brained senator from South Carolina has once again (shock! surprise!) swerved to the left in an effort to advance his personal interests.</p>
<blockquote><p>And so, last Friday, in stumbled Sens. Lindsey Graham, John Thune, Saxby Chambliss, Bob Corker and Johnny Isakson &#8212; alongside five Senate Democrats. This &#8220;Gang of 10&#8243; announced a &#8220;sweeping&#8221; and &#8220;bipartisan&#8221; energy plan to break Washington&#8217;s energy &#8220;stalemate.&#8221; What they did was throw every vulnerable Democrat, and Mr. Obama, a life preserver.</p></blockquote>
<p><a href="http://online.wsj.com/public/article_print/SB121815293390922431.html">Here are the dirty details from the WSJ.</a></p>
<p>Way to go Lindz, you really know what the people want.</p>
]]></content:encoded>
			<wfw:commentRss>http://webguru.org/2008/08/14/politics/why-doesnt-lindsey-graham-just-go-ahead-and-claim-the-d/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to use PHP&#8217;s sprintf() on a MySQL query utilizing DATE_FORMAT()</title>
		<link>http://webguru.org/2008/08/02/php/how-to-use-phps-sprintf-on-a-mysql-query-utilizing-date_format/</link>
		<comments>http://webguru.org/2008/08/02/php/how-to-use-phps-sprintf-on-a-mysql-query-utilizing-date_format/#comments</comments>
		<pubDate>Sat, 02 Aug 2008 15:24:28 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://webguru.org/2008/08/02/php/how-to-use-phps-sprintf-on-a-mysql-query-utilizing-date_format/</guid>
		<description><![CDATA[It is good practice (common sense?) to filter your SQL queries.  One way to accomplish this in PHP is to utilize a function like sprintf() which will format a given string and integrate values into the string using conversion specifications which are passed in as arguments to the function.  In plain english, that means you [...]]]></description>
			<content:encoded><![CDATA[<p>It is good practice (common sense?) to filter your SQL queries.  One way to accomplish this in PHP is to utilize a function like sprintf() which will format a given string and integrate values into the string using conversion specifications which are passed in as arguments to the function.  In plain english, that means you can call the function, pass in a value and require that value to be a integer, for example.  If the value you passed in is a string, roughly speaking, it will sanitize your output.</p>
<p>An example in a MySQL query would be this.</p>
<pre class="brush: php;">
&lt;?php
// build our sql string.
$sql = &quot;SELECT * FROM table WHERE field=%d&quot;;
$sqlf = sprintf( $sql, $somevalue );
$db-&gt;query($sqlf);
?&gt;
</pre>
<p>As you can see, you can designate where the substitution will take place in the $sql string.  That&#8217;s easy.  But what happens if you need to use MySQL&#8217;s DATE_FORMAT() function?  It requires that you pass in arguments to define its output (ie. Day as a word, day as a date, month as a number, etc).</p>
<pre class="brush: php;">
&lt;?php
// build our sql string.
$sql = &quot;SELECT DATE_FORMAT( %b %M %d %Y, some_date_field ) as myDate FROM table WHERE field=%d&quot;;
$sqlf = sprintf( $sql, $somevalue );
$db-&gt;query($sqlf);
?&gt;
</pre>
<p>This will fail.  sprintf() will complain because you haven&#8217;t passed in enough arguments.  It is expecting 5 values as part of the call, instead of just the one that you are trying to replace (in the SQL WHERE clause).</p>
<p>So what&#8217;s the solution? You have to &#8220;comment-out&#8221; the % that aren&#8217;t part of your sprintf() substitution.  You can do this by putting another % in front of the &#8216;%&#8217; symbols in the DATE_FORMAT() function.  This deems them as a literal percent-sign instead of the start of another sprintf() &#8220;variable&#8221;.</p>
<pre class="brush: php;">
&lt;?php
// build our sql string.
$sql = &quot;SELECT DATE_FORMAT( %%b %%M %%d %%Y, some_date_field ) as myDate FROM table WHERE field=%d&quot;;
$sqlf = sprintf( $sql, $somevalue );
$db-&gt;query($sqlf);
</pre>
<p>Hope that helps!</p>
]]></content:encoded>
			<wfw:commentRss>http://webguru.org/2008/08/02/php/how-to-use-phps-sprintf-on-a-mysql-query-utilizing-date_format/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>I may need protection from myself&#8230;</title>
		<link>http://webguru.org/2008/08/01/php/i-may-need-protection-from-myself/</link>
		<comments>http://webguru.org/2008/08/01/php/i-may-need-protection-from-myself/#comments</comments>
		<pubDate>Fri, 01 Aug 2008 14:22:21 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://webguru.org/2008/08/01/php/i-may-need-protection-from-myself/</guid>
		<description><![CDATA[Today, I spent almost an hour staring at this line of code:
if ($this-&#62;dbi-&#62;affectedRows&#62;0) {
Oh &#8211; and it would be helpful for you to know that affectedRows is a method in a database class.
A method, that&#8217;s right.  Unfortunately for me, method is not spelled, v-a-r-i-a-b-l-e.
Amazing what two little ( ) can do to your stress level.
if [...]]]></description>
			<content:encoded><![CDATA[<p>Today, I spent almost an hour staring at this line of code:<br />
<code>if ($this-&gt;dbi-&gt;affectedRows&gt;0) {</code></p>
<p>Oh &#8211; and it would be helpful for you to know that affectedRows is a method in a database class.</p>
<p>A <em>method</em>, that&#8217;s right.  Unfortunately for me, method is not spelled, v-a-r-i-a-b-l-e.</p>
<p>Amazing what two little ( ) can do to your stress level.<br />
<code>if ($this-&gt;dbi-&gt;affectedRows<font color="#ff0000"><strong>()</strong></font>&gt;0) {</code></p>
]]></content:encoded>
			<wfw:commentRss>http://webguru.org/2008/08/01/php/i-may-need-protection-from-myself/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Replacement Batteries for APC SU1000XLNET and BU550R</title>
		<link>http://webguru.org/2008/07/21/general/replacement-batteries-for-apc-su1000xlnet-and-bu550r/</link>
		<comments>http://webguru.org/2008/07/21/general/replacement-batteries-for-apc-su1000xlnet-and-bu550r/#comments</comments>
		<pubDate>Mon, 21 Jul 2008 18:47:35 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Sysadmin]]></category>
		<category><![CDATA[apc]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[batteries]]></category>
		<category><![CDATA[bu550r]]></category>
		<category><![CDATA[gruber power]]></category>
		<category><![CDATA[rbc11]]></category>
		<category><![CDATA[rbc2]]></category>
		<category><![CDATA[rbc7]]></category>
		<category><![CDATA[replacement]]></category>
		<category><![CDATA[su1000xlnet]]></category>
		<category><![CDATA[ups]]></category>

		<guid isPermaLink="false">http://webguru.org/2008/07/21/general/replacement-batteries-for-apc-su1000xlnet-and-bu550r/</guid>
		<description><![CDATA[I needed to replace the batteries in my APC SmartUPS 1000XL battery backup, its external battery pack (SU24XLBP) and a smaller system, the BackupUPS ES 550.  After looking at the options on &#8220;official&#8221; replacements from APC and consequently realizing that money is not currently growing on the primarily ornamental trees in my yard; I decided [...]]]></description>
			<content:encoded><![CDATA[<p>I needed to replace the batteries in my APC SmartUPS 1000XL battery backup, its external battery pack (SU24XLBP) and a smaller system, the BackupUPS ES 550.  After looking at the options on &#8220;official&#8221; replacements from APC and consequently realizing that money is not currently growing on the primarily ornamental trees in my yard; I decided to peruse other options.</p>
<p>There are many places on the &#8216;net that you can find &#8220;comparable&#8221; batteries for your APC (and other) backup units.  Knowing that my friend <a href="http://cerient.net" title="Cerient Technologies">Jason</a> replaces battery backups with some frequency, I asked for his opinion.  He led me to <a href="http://www.gruberpower.com" title="Gruber Power Services">Gruber Power Services</a>.  What a find!</p>
<p align="center"><a href="http://www.gruberpower.com/index.htm" title="Gruber Power Services"><img src="http://content.screencast.com/media/59fafd99-418c-4535-83eb-c2ff5c827c14_e05c80de-13f2-4cd2-9256-5288ae1d27ce_static_0_0_2008-07-21_1436.png" border="0" width="595" height="148" /></a></p>
<p>The prices are extremely reasonable (e.g. $15.99 for a &#8220;RBC2&#8243; comparable battery which is $40 through APC and other sites). You obviously have to pay for shipping these heavy lead-acid batteries, but that is the case with any site from which you order.  Shipping for the entire order ended up being about $100 bucks (ugh!).</p>
<p>All told, I estimate that I saved about $350 (incl. shipping) by ordering from Gruber.  Assuming the rest of my experience matches those of my friend, I highly recommend them.</p>
<p><strong>UPDATE (7/23/08):</strong> Got a personal email this morning from &#8220;Dawn&#8221; thanking me for my order and providing to me the UPS tracking numbers for my shipment.  Pretty cool.</p>
<p><strong>UPDATE (7/30/08):</strong> The batteries were delivered Monday, as projected.  I have installed one of them so far and it fit well, connected easily and seems to be a great product.</p>
]]></content:encoded>
			<wfw:commentRss>http://webguru.org/2008/07/21/general/replacement-batteries-for-apc-su1000xlnet-and-bu550r/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mapping the Future Route of I-540/NC540 with Google Maps</title>
		<link>http://webguru.org/2008/07/17/general/mapping-the-future-route-of-i-540nc540-with-google-maps/</link>
		<comments>http://webguru.org/2008/07/17/general/mapping-the-future-route-of-i-540nc540-with-google-maps/#comments</comments>
		<pubDate>Thu, 17 Jul 2008 18:12:08 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[540]]></category>
		<category><![CDATA[eastern wake]]></category>
		<category><![CDATA[expressway]]></category>
		<category><![CDATA[future]]></category>
		<category><![CDATA[I-540]]></category>
		<category><![CDATA[I540]]></category>
		<category><![CDATA[NC 540]]></category>
		<category><![CDATA[NC DOT]]></category>
		<category><![CDATA[NC540]]></category>
		<category><![CDATA[outer belt]]></category>
		<category><![CDATA[projected]]></category>
		<category><![CDATA[route]]></category>
		<category><![CDATA[southern wake]]></category>
		<category><![CDATA[toll road]]></category>

		<guid isPermaLink="false">http://webguru.org/2008/07/17/general/mapping-the-future-route-of-i-540nc540-with-google-maps/</guid>
		<description><![CDATA[View I-540/NC540 Proposed Routes in a larger map
I&#8217;m very interested in where I-540/NC540 will be built (assuming NC comes up with the money, and by that I mean raises taxes, builds toll booths and/or Gov. Mike Easley stops robbing the Highway Trust Fund).  The Southern Wake Expressway and the Eastern Wake Expressway will be a [...]]]></description>
			<content:encoded><![CDATA[<p><iframe width="575" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps/ms?ie=UTF8&amp;hl=en&amp;msa=0&amp;msid=116409505554706824342.000452396276f8838dc72&amp;t=p&amp;ll=35.73237,-78.699712&amp;spn=0.22252,0.388665&amp;output=embed"></iframe><br /><small>View <a href="http://maps.google.com/maps/ms?ie=UTF8&amp;hl=en&amp;msa=0&amp;msid=116409505554706824342.000452396276f8838dc72&amp;t=p&amp;ll=35.73237,-78.699712&amp;spn=0.22252,0.388665&amp;source=embed" style="color:#0000FF;text-align:left">I-540/NC540 Proposed Routes</a> in a larger map</small></p>
<p>I&#8217;m very interested in where I-540/NC540 will be built (assuming NC comes up with the money, and by that I mean raises taxes, builds toll booths and/or Gov. Mike Easley stops robbing the Highway Trust Fund).  The Southern Wake Expressway and the Eastern Wake Expressway will be a great boon to the area and will make it so much easier to get around the triangle if you live in the increasingly popular corridor between Cary and Fuquay-Varina.  So, I did some searching and found the projected routes on the <a title="NC DOT Projects Website" href="https://apps.dot.state.nc.us/projects/tipsearch/Default.aspx">NC DOT Projects website</a>.</p>
<p>I&#8217;ve transferred what I found there on various maps into Google Maps.  The route should be pretty close to what is shown on the planning maps from DOT.  I used the satellite view from Google Maps and my own common sense to determine where certain road and property crossing would take place.  For example, I made the assumption that they would snake the road in and around established neighborhoods.  I could be wrong, but I think it is a pretty close approximation of the anticipated route.</p>
<p>Google Maps allows you to export a custom map into a KMZ file, suitable for viewing in Google Earth.  I am making that file <a title="I-540/NC540 Future Route KMZ File" href="/wp-content/uploads/2008/07/I-540_NC540_route.kmz">available here as well</a>.</p>
<p><a href="http://content.screencast.com/media/ba902571-6b75-4c04-a152-3ec376ecf563_e05c80de-13f2-4cd2-9256-5288ae1d27ce_static_0_0_2008-07-17_1343.png"><br />
</a></p>
]]></content:encoded>
			<wfw:commentRss>http://webguru.org/2008/07/17/general/mapping-the-future-route-of-i-540nc540-with-google-maps/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

