<?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>Josh Duff &#187; Josh</title>
	<atom:link href="http://joshduff.com/author/admin/feed" rel="self" type="application/rss+xml" />
	<link>http://joshduff.com</link>
	<description>A guy with a web site</description>
	<lastBuildDate>Wed, 25 Apr 2012 14:54:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>How Should We Then Drive?</title>
		<link>http://joshduff.com/323/how-should-we-then-drive</link>
		<comments>http://joshduff.com/323/how-should-we-then-drive#comments</comments>
		<pubDate>Sat, 24 Sep 2011 21:00:24 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[Deep thoughts]]></category>

		<guid isPermaLink="false">http://joshduff.com/?p=323</guid>
		<description><![CDATA[Inside joke: the title of this post is a reference to a book I have never actually read I believe that God created the world, and that those who are chosen by him should obey him to the best of their abilities.  Despite these aspirations, I&#039;m a terrible sinner. Nonetheless, at least I&#039;ve got goals [...]]]></description>
			<content:encoded><![CDATA[<p><em>Inside joke: the title of this post is a reference to <a href="http://en.wikipedia.org/wiki/How_Should_We_Then_Live%3F">a book</a> I have never actually read</em></p>
<p>I believe that God created the world, and that those who are <a href="http://en.wikipedia.org/wiki/Limited_atonement">chosen</a> by him should obey him to the best of their abilities.  Despite these aspirations, I&#039;m a terrible sinner.</p>
<p>Nonetheless, at least I&#039;ve got goals to aspire to, right?  Sure, there are quite a few that most people don&#039;t agree with, especially <a href="http://www.biblegateway.com/passage/?search=leviticus%2018:22&amp;version=MSG">the ones that prohibit actions that non-Christians (and even many self-proclaimed Christians) don&#039;t want to call evil</a>.</p>
<p>Still, most people are on board with the second-greatest commandment &#8211; in fact, <a href="http://store.penny-arcade.com/products/pat070181">according to popular culture</a>, that is in fact the main point of Jesus&#039; coming to Earth!</p>
<blockquote><p>37 Jesus said to him, &#034;&#039;You shall love the LORD your God with all your heart, with all your soul, and with all your mind.&#039;</p>
<p>38 This is the first and great commandment.</p>
<p>39 And the second is like it: &#039;You shall love your neighbor as yourself.&#039;</p>
<p>40 On these two commandments hang all the Law and the Prophets.&#034;</p>
<p><a href="http://www.biblegateway.com/passage/?search=matthew%2022:37-40&amp;version=MSG">Matthew 22:37-40</a> (NKJV)</p></blockquote>
<p>(And for those paying attention, yes, I consider Penny Arcade to be an arbiter of popular culture.)</p>
<p>But if I&#039;m not engaging in weekly gay-hunts, what IS my Christianity doing for me?  Well, among the many other instructions for living life that God passed down to us, it seems that he really does care about Christians being an improvement for the quality-of-life of the people around us:</p>
<blockquote><p>&#034;Let me tell you why you are here. You&#039;re here to be salt-seasoning that brings out the God-flavors of this earth. If you lose your saltiness, how will people taste godliness? You&#039;ve lost your usefulness and will end up in the garbage.&#034; <a href="http://www.biblegateway.com/passage/?search=matthew%205:13&amp;version=MSG">Matthew 5:13</a> (MSG)</p></blockquote>
<h1>Seriously, what is this post even about</h1>
<p>All right, so Christians aren&#039;t supposed to be assholes.  Most people sort of assume that already, even given plenty of implicit counter-arguments via the actions of Christians they know.</p>
<p>However, I&#039;ve noticed a high correlation between driving a vehicle on the road and anger.  While driving as a passenger, I&#039;ve noticed that it is extremely common for a driver to express anger at</p>
<ul>
<li>The drivers sharing the road with him</li>
<li>Shitty things that happen while driving (inconvenient red lights, poor road signs, etc)</li>
</ul>
<p>The former is by far the most common &#8211; the other vehicles on the road have a tendency to cut you off, slow you down, fail to announce where they are going, and generally just act like jerks.</p>
<h2>Why does that burn people up so much?</h2>
<p>If I accidentally bump my shopping cart into someone else at the grocery store, I&#039;ll quickly apologize &#8211; and the odds are that they&#039;ll say something to the effect of &#034;ah, don&#039;t worry&#034; or give an understanding smile, or at worst give a small huff and push on in annoyance.</p>
<p>If I turn left on a street and someone in oncoming traffic has to slow down to avoid me safely, I don&#039;t usually get a chance to hear their response.  I&#039;ve driven with enough people to imagine it, though &#8211; when you see someone doing something stupid or unnecessary in a way that inconveniences you, you generally feel a quick flash of anger toward them, and may say something to that effect at their expense.</p>
<p>Maybe it&#039;s because you just see vehicles &#8211; you&#039;re not dealing with humans, you&#039;re dealing with 4 wheels and a bumper sticker pushing [political party you hate]!  Most people are polite enough to be diplomatic in the face of being inconvenienced by another human face, but if a hunk of metal cuts you off, you feel real rage at the theoretical person inside of it!</p>
<h1>I&#039;m glad I&#039;m smarter than that!</h1>
<p>Oh wait, no, I get mad exactly like that, all the time.  Despite the Bible hammering in the fact that <a href="http://www.openbible.info/topics/self-control">God expects you to be working on this self-control thing</a>, I&#039;ve raged at other drivers about 100% more than I should have.  What am I supposed to do, turn every driving excursion into a painful turn-the-other-cheek exercise?</p>
<p>Well, that has kind of happened &#8211; but not until after I started adopted a proactive stance towards driving smart.</p>
<p>My life was changed (no, really!) when I read <a href="http://trafficwaves.org/">this site</a> written by an engineer on the internet.  I&#039;d recommend reading it over, but here&#039;s the basic premise:</p>
<ul>
<li>Busy roads (especially big ones like highways) get inefficient to travel on quite a bit</li>
<li>It turns out that if you drive in a certain way, you can make highway driving a lot more efficient&#8230; not for you, but for all the people behind you, too</li>
</ul>
<p>So how does a motivated engineer make the world better?  It turns out, you can improve the quality-of-life of those around you by simply <strong>leaving a big gap between you and the vehicle in front of you</strong>.</p>
<p>I don&#039;t want to belabor all of the points he makes (seriously, you should read his site), but the general idea is</p>
<ul>
<li>If you never hit your brakes, the people behind you never have to hit their brakes, which lets everyone avoid the burst-forward-then-come-to-nearly-a-complete-stop situation that happens way too often on busy roads</li>
<li>It&#039;s easy for people to merge in in front of you, which lets the lanes next to you avoid the someone-driving-really-slow-trying-to-merge-lanes situation</li>
</ul>
<h1>Is that really how you drive like a Christian?</h1>
<p>So you&#039;re leaving a carefully-engineered gap between you and the traffic in front of you.  Traffic behind you is demonstrably better because of it.  What&#039;s the big deal?</p>
<p>Well, it turns out that this habit is not natural at all.  At least not to me.  When did I turn into a competitive driver looking to beat everyone else to my exit?  I&#039;m not sure.  Maybe it was the day I got my drivers license.  Maybe it was the day I was born with two working testicles.  I haven&#039;t been able to pin it down.</p>
<h2>They just&#8230; keep&#8230; merging!</h2>
<p>People will merge in front of you.  And man, no matter how zen I&#039;ve been feeling about driving lately, my first instinct is STILL &#034;gotta close the gap so the rest of the horde doesn&#039;t jump in in front of me!&#034;</p>
<p>Calm yourself with these facts: each car who merges in front of you (potentially forcing you to let up on the accelerator in order to maintain good distance between you and the car ahead) adds&#8230; maybe 5 seconds to your trip.  Sure, there&#039;s a slim chance that those 5 seconds could cause you to miss a light somewhere, but aren&#039;t those low odds worth improving the driving situation for all those around you and behind you?</p>
<p>Don&#039;t forget: the easier it is for people to merge into your lane, the smoother traffic will be for all of the people behind you!</p>
<h2>Not dying is good, too</h2>
<p>So yeah, this falls by the wayside whenever people are in a hurry, but it turns out that the more space you leave between you and the vehicle in front of you, the safer you are.</p>
<p>I&#039;m struggling not to write a 500-word rant on this topic alone.  Two seconds to respond to sudden developments ahead of you makes a massive difference compared to one second&#8230; and protip: you&#039;ve been counting seconds too fast.  Giving yourself a larger margin of error turns &#034;accidents&#034; into much safer near-misses.</p>
<p>But keeping your car and skull dent-free are just happy side effects that can happen when you decide to try to apply the biblical principle of making-life-better-for-others to driving.</p>
<p>Resist the power of <a href="http://www.penny-arcade.com/comic/2004/3/19/">John Gabriel&#039;s Greater Internet Fuckwad Theory</a> &#8211; start training yourself to be a blessing to the anonymized people on the road around you!</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduff.com/323/how-should-we-then-drive/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why you should NOT be using mysqli::prepare</title>
		<link>http://joshduff.com/270/why-you-should-not-be-using-mysqli-prepare</link>
		<comments>http://joshduff.com/270/why-you-should-not-be-using-mysqli-prepare#comments</comments>
		<pubDate>Mon, 09 May 2011 05:33:15 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://joshduff.com/?p=270</guid>
		<description><![CDATA[If you&#039;re writing PHP code that works with a MySQL database, there&#039;s a good chance you&#039;ll have heard about the spiffy &#034;prepared queries&#034; functionality available with the mysqli library. &#034;It&#039;s totally awesome!&#034; some people will say &#8211; it makes queries easier to read in code, it handles all of the escaping for you (yay for [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#039;re writing PHP code that works with a MySQL database, there&#039;s a good chance you&#039;ll have heard about the spiffy &#034;prepared queries&#034; functionality available with the mysqli library.  &#034;It&#039;s totally awesome!&#034; some people will say &#8211; it makes queries easier to read in code, it handles all of the escaping for you (yay for easy security!), and it&#039;s way more efficient if you&#039;re running the same query a bunch of times.</p>
<p>Here&#039;s an example of how you might use the <a href="http://us.php.net/manual/en/mysqli.prepare.php">prepared query functionality in PHP</a> (straight from the docs!):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mysqli</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT District FROM City WHERE Name=?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bind_param</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;s&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$city</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bind_result</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$district</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%s</span> is in district <span style="color: #009933; font-weight: bold;">%s</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$city</span><span style="color: #339933;">,</span> <span style="color: #000088;">$district</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It&#039;s a pretty reasonable-looking way of building a query.  Here&#039;s how you would have to build that query normally:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$mysqli</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT District FROM City WHERE Name='&quot;</span> 
	<span style="color: #339933;">.</span> <span style="color: #000088;">$mysqli</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$city</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Once you want to start using a lot of random static strings in your query, it can get pretty messy &#8211; having to concatenate all of those strings together, calling that escape function everywhere &#8211; using the bind_param function makes things a lot more readable.</p>
<h2>IF YOU&#039;RE AN IDIOT!</h2>
<p>No wait, hear me out, I didn&#039;t mean it like that &#8211; what I meant to say is, the prepared query syntax is probably doing more work than you expect it to.</p>
<p>See, prepared statements are actually a SQL thing, not something that the PHP devs added to the mysqli wrapper just because they thought it would be cool.  You can use prepared statements from the MySQL command line, too:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;">PREPARE my_statement <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #008000;">'SELECT District FROM City WHERE Name=?'</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">SET</span> @city <span style="color: #CC0099;">:=</span> <span style="color: #008000;">'Amersfoort'</span><span style="color: #000033;">;</span>
EXECUTE my_statement <span style="color: #990099; font-weight: bold;">USING</span> @city<span style="color: #000033;">;</span>
DEALLOCATE PREPARE my_statement<span style="color: #000033;">;</span></pre></div></div>

<p>mysqli::prepare is just giving you access to this nifty feature, which is fine and dandy.  However, you may have noticed something -</p>
<h2>That&#039;s a lot of queries to run, just to run a single SELECT query!</h2>
<p>Now, if you&#039;re going to be running a ton of INSERT or UPDATE queries, and you just want to swap the variables out each time, you&#039;re fine &#8211; letting MySQL hang on to the meat of a query you&#039;re going to be running a hundred times is downright reasonable.</p>
<p>BUT &#8211; if all you&#039;re doing is running a half dozen queries while you&#039;re building a web page to display to the user, <strong>the only thing you&#039;re doing is increasing the number of times you have to contact the server.</strong></p>
<p>How many superfluous database-contacts will there be?  As far as I can tell, you&#039;ll be contacting the server three times more than you need to:</p>
<ul>
<li>Once to prepare the statement</li>
<li>Once to send the parameters to the server and get the results back</li>
<li>Once to deallocate the statement</li>
</ul>
<p><em>(I haven&#039;t looked at the source code, but I&#039;m guessing that <a href="http://dev.mysql.com/doc/refman/5.5/en/mysql-stmt-bind-param.html">commenter Angus M is correct in his assessment</a> that binding a parameter does not cause communication with the database.)</em></p>
<p>If your database server is running on the same host as your web server, the cost of contacting the database is probably pretty low.  Still, after years of developing thinnish-desktop clients, my instinct is to avoid that overhead wherever possible.</p>
<h2>Stuck between a rock and an inefficient place</h2>
<p>I imagine that, among the people who took those last few paragraphs seriously, there are two camps &#8211; the coders who will say &#034;well, whatever&#034; and keep writing queries using the prepare syntax because it&#039;s more convenient and easier to read (which is an argument I can understand), and the efficiency advocates who will give a tortured sigh and go back to writing queries with lots of awkward concatenation, in order to avoid overhead.</p>
<p>Or you could write/copy some simple query-building tools that do the stuff that you originally thought mysqli::prepare was doing!</p>
<p>Here&#039;s a function I threw together last night, which I imagine would suit the needs of many developers using PHP/MySQL:

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> GetQueryWithData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$Query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$ParameterNumber</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">func_num_args</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$Query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">func_get_arg</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ParameterNumber</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ParameterNumber</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">func_num_args</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;">$NextParameter</span> <span style="color: #339933;">=</span> <span style="color: #990000;">func_get_arg</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ParameterNumber</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$PlaceToInsertParameter</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Query</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'?'</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;">$PlaceToInsertParameter</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$QuerySafeString</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_bool</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$NextParameter</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$QuerySafeString</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$NextParameter</span> ? <span style="color: #0000ff;">'TRUE'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'FALSE'</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_float</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$NextParameter</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #990000;">is_int</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$NextParameter</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$QuerySafeString</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$NextParameter</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_null</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$NextParameter</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$QuerySafeString</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'NULL'</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #b1b100;">else</span>
				<span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$QuerySafeString</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;'&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$NextParameter</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
&nbsp;
				<span style="color: #000088;">$Query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr_replace</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Query</span><span style="color: #339933;">,</span> <span style="color: #000088;">$QuerySafeString</span><span style="color: #339933;">,</span> <span style="color: #000088;">$PlaceToInsertParameter</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$Query</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">print</span> GetQueryWithData<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM `ass` WHERE `butt` = ? AND `cheek_id` = ? AND ? &quot;</span>
	<span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;AND `nullable_field` IS ? &quot;</span><span style="color: #339933;">,</span> 
	<span style="color: #0000ff;">&quot;lol 'WUT'&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">13</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* SELECT * FROM `ass` WHERE `butt` = 'lol \'WUT\'' AND `cheek_id` = 13 AND TRUE 
	AND `nullable_field` IS NULL */</span>
&nbsp;
<span style="color: #b1b100;">print</span> GetQueryWithData<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'UPDATE `some_table` SET `some_column` = ?, `some_other_column` = ?, '</span>
	<span style="color: #339933;">.</span> <span style="color: #0000ff;">'`some_id` = ? WHERE `yourface` = ?'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'20'</span><span style="color: #339933;">,</span> 
	<span style="color: #cc66cc;">21</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">69</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'sucks'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* UPDATE `some_table` SET `some_column` = '20', `some_other_column` = 21, 
	`some_id` = 69 WHERE `yourface` = 'sucks' */</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The above function is a simple way to build a query with any number of parameters.  Strings will be escaped, numbers will not be quoted, and the function never contacts the database server.</p>
<p>Using this function, you can redux the query from the original example while only contacting the database once:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$mysqli</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span>GetQueryWithData<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT District FROM City WHERE Name=?&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$city</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If you don&#039;t want to use that function, or roll your own query-builder, you can keep using mysqli&#039;s wrapper for prepared statements &#8211; just know that it was made for a different purpose, and it comes with a bit more cost.</p>
<h2>Edit: PDO!</h2>
<p>After some discussion with <a href="http://php.ss23.geek.nz/">ss23</a> in #mysql, I thought I&#039;d better add some comments about <a href="http://php.net/pdo">the PDO class</a>.</p>
<p>On the surface (and according to much of the documentation), it appears to be a more generic (not just MySQL-specific) database wrapper with similar prepared-statement support.  However, it turns out that by default, <a href="http://bugs.php.net/bug.php?id=54638">it only fakes prepared statements</a>!</p>
<p>So, it is possible to use PDO to write queries using a bind-parameter syntax that can make your code a lot easier to read, and simultaneously avoid contacting the database too often.  However, if that is how you decide to roll, I would recommend turning on fake-prepared-statements mode explicitly, just in case that default gets changed in the future:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$pdo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttribute</span><span style="color: #009900;">&#40;</span>PDO<span style="color: #339933;">::</span><span style="color: #004000;">ATTR_EMULATE_PREPARES</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://joshduff.com/270/why-you-should-not-be-using-mysqli-prepare/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>What would universities do if they cared about computer science?</title>
		<link>http://joshduff.com/252/what-would-universities-do-if-they-cared-about-computer-science</link>
		<comments>http://joshduff.com/252/what-would-universities-do-if-they-cared-about-computer-science#comments</comments>
		<pubDate>Sat, 08 Jan 2011 07:19:45 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[Deep thoughts]]></category>

		<guid isPermaLink="false">http://joshduff.com/?p=252</guid>
		<description><![CDATA[When I say computer science, I mean the practical kind &#8211; where people are writing useful software that will be used by someone else. If universities really cared about educating their students, they would take an audit once a year (at least) to take scope of the current programming world. They would look at the [...]]]></description>
			<content:encoded><![CDATA[<p>When I say computer science, I mean the practical kind &#8211; where people are writing useful software that will be used by someone else.</p>
<p>If universities really cared about educating their students, they would take an audit once a year (at least) to take scope of the current programming world.</p>
<p>They would look at the currently available development platforms, and identify the ones that were well-established and provided the best introduction to tools that could solve real-world problems that real-world application developers were working on.</p>
<p>Then they would go out and hire professionals with real experience in these languages (!) to train the students in using these tools without sucking.</p>
<p>This would comprise most of the third and fourth year studies for computer scientists (or whatever bachelor&#039;s degree that would represent &#034;practical programming&#034;).</p>
<p>But wait, what about the first two years?  Oh, well that would be spent teaching developers to work on projects in a way that would make them (the software projects) not suck.</p>
<p>Everyone initially sucks at writing software that coders (even themselves) will have to maintain.  It generally takes at least a year or two to work the worst of these symptoms out of your system.</p>
<p>If universities focused on the practical, they would work on beating that stupidity out of every student who wanted to write software.</p>
<p>If universities were practical, while simultaneously forward-thinking, they would smack the stupidity out of students while *also* training them on systems that real companies were using to solve current problems.</p>
<p>Based on my limited education, I note that universities spend most of their efforts on the completely irrelevant (general electives, ho!) and mathematical work that is only useful to coders who are already well-educated.</p>
<p>But don&#039;t take my opinion too seriously &#8211; I am a college dropout, after all.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduff.com/252/what-would-universities-do-if-they-cared-about-computer-science/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>On Growing as a Programmer (I think)</title>
		<link>http://joshduff.com/248/on-growing-as-a-programmer-i-think</link>
		<comments>http://joshduff.com/248/on-growing-as-a-programmer-i-think#comments</comments>
		<pubDate>Sat, 21 Aug 2010 22:28:04 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[Deep thoughts]]></category>

		<guid isPermaLink="false">http://joshduff.com/?p=248</guid>
		<description><![CDATA[I keep hoping that some day, I will look back at code I wrote over a year prior without once thinking &#034;man, I was young and stupid back then&#034;. That day has not arrived yet. Perhaps I should be consoled by the fact that the reason I&#039;m looking at these prior works now isn&#039;t because [...]]]></description>
			<content:encoded><![CDATA[<p>I keep hoping that some day, I will look back at code I wrote over a year prior without once thinking &#034;man, I was young and stupid back then&#034;.  That day has not arrived yet.</p>
<p>Perhaps I should be consoled by the fact that the reason I&#039;m looking at these prior works now isn&#039;t because they broke in front of a customer.  This time, at least.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduff.com/248/on-growing-as-a-programmer-i-think/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL wrapper class (PHP)</title>
		<link>http://joshduff.com/232/mysql-wrapper-class-php</link>
		<comments>http://joshduff.com/232/mysql-wrapper-class-php#comments</comments>
		<pubDate>Mon, 26 Jul 2010 23:07:46 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://joshduff.com/?p=232</guid>
		<description><![CDATA[I&#039;m not saying that this is the best implementation of a MySQL wrapper class for PHP. I&#039;m not advocating its use in production code. However, this is what I&#039;ve used in most scripts I&#039;ve thrown together in the last few years &#8211; I hate having to throw a mysql_error call after every query, y&#039;know? &#60;?php [...]]]></description>
			<content:encoded><![CDATA[<p>I&#039;m not saying that this is the best implementation of a MySQL wrapper class for PHP.  I&#039;m not advocating its use in production code.</p>
<p>However, this is what I&#039;ve used in most scripts I&#039;ve thrown together in the last few years &#8211; I hate having to throw a mysql_error call after every query, y&#039;know?</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'CLIENT_LONG_PASSWORD'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MySQLWrapper
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$server_port</span>	<span style="color: #339933;">=</span>		<span style="color: #0000ff;">'localhost:3306'</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$database</span> 		<span style="color: #339933;">=</span>		<span style="color: #0000ff;">'whatever'</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$username</span> 		<span style="color: #339933;">=</span>		<span style="color: #0000ff;">'a username'</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$password</span> 		<span style="color: #339933;">=</span>		<span style="color: #0000ff;">'a password'</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$force_long_password</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$db_object</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$server_port</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$database</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$username</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$force_long_password</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">server_port</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$server_port</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$database</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$username</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">password</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$password</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">force_long_password</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$force_long_password</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">server_port</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Connect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> Connect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db_object</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">mysql_ping</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db_object</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db_object</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Connect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$db_object</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">server_port</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">password</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">force_long_password</span> ? CLIENT_LONG_PASSWORD <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: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">''</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Select database failed for <span style="color: #006699; font-weight: bold;">$this-&gt;database</span>: <span style="color: #006699; font-weight: bold;">$result</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> Disconnect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$return</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db_object</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$return</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db_object</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db_object</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$return</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> Execute<span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Connect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_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>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Query failed: <span style="color: #006699; font-weight: bold;">$result</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Teh query itself: <span style="color: #006699; font-weight: bold;">$query</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>You may find this slightly better than using PHP&#039;s MySQL functions straight out of the box.  Or not.  Who can say?</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduff.com/232/mysql-wrapper-class-php/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>This program has no useful purpose, so I posted it on my blog</title>
		<link>http://joshduff.com/235/this-program-has-no-useful-purpose-so-i-posted-it-on-my-blog</link>
		<comments>http://joshduff.com/235/this-program-has-no-useful-purpose-so-i-posted-it-on-my-blog#comments</comments>
		<pubDate>Mon, 26 Jul 2010 23:06:56 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://joshduff.com/?p=235</guid>
		<description><![CDATA[Look, this is just dumb. I don&#039;t even know why I felt like writing this. This script will keep a certain percentage of the tables in your MySQL database locked. It will lock the first N% of your database, and then move forward one table at a time, locking the next table in line and [...]]]></description>
			<content:encoded><![CDATA[<p>Look, this is just dumb.</p>
<p>I don&#039;t even know why I felt like writing this.</p>
<p>This script will keep a certain percentage of the tables in your MySQL database locked.  It will lock the first N% of your database, and then move forward one table at a time, locking the next table in line and releasing the lock on the one at the end of the tail.</p>
<p>Why would someone use this script?  For benchmarking some completely unrealistic scenario?  To decrease database efficiency for no good reason?  Who can say.</p>
<p>Note: you&#039;ll get to lock/release tables a lot more quickly if you run this script locally.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$percentage_of_database_to_lock</span> <span style="color: #339933;">=</span> <span style="color:#800080;">.25</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'./shared/mysql.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$mysql</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MySQLWrapper<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mysql</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">server_port</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'host:3306'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mysql</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'some_db'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mysql</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'some_user'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mysql</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">password</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'some_pass'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mysql</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Execute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SHOW TABLES'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$tables</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;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</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_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$tables</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$tables_to_lock</span> <span style="color: #339933;">=</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$percentage_of_database_to_lock</span> <span style="color: #339933;">*</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tables</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$tables_to_lock</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$cycles</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$start_at</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'LOCK TABLE'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$tables_so_far</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$tables_so_far</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$tables_to_lock</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$next_index</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$start_at</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$tables_so_far</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$next_index</span> <span style="color: #339933;">&gt;=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tables</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$next_index</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$next_index</span> <span style="color: #339933;">-</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tables</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #000088;">$query</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'`'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$tables</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$next_index</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'` WRITE, '</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$tables_so_far</span><span style="color: #339933;">++;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$mysql</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Execute</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;">$start_at</span><span style="color: #339933;">++;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$start_at</span> <span style="color: #339933;">&gt;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tables</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$start_at</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Cycle &quot;</span> <span style="color: #339933;">.</span> <span style="color: #339933;">++</span><span style="color: #000088;">$cycles</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Dependencies: <a href="http://joshduff.com/232">MySQLWrapper</a></p>
]]></content:encoded>
			<wfw:commentRss>http://joshduff.com/235/this-program-has-no-useful-purpose-so-i-posted-it-on-my-blog/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Profanities and other funny words</title>
		<link>http://joshduff.com/203/profanities-and-other-funny-words</link>
		<comments>http://joshduff.com/203/profanities-and-other-funny-words#comments</comments>
		<pubDate>Sat, 19 Jun 2010 22:11:49 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://joshduff.com/?p=203</guid>
		<description><![CDATA[I recently did a quick audit of the source code I help maintain/develop for the company I work for.  By which I mean, I spent the last part of a Friday searching through our code for funny words. I decided to open up by looking for traditional profanities &#8211; a commonly-accepted method of gauging the [...]]]></description>
			<content:encoded><![CDATA[<p>I recently did a quick audit of the source code I help maintain/develop for <a href="http://wikido.isoftdata.com/index.php/ISoft_Data_Systems">the company I work for</a>.  By which I mean, I spent the last part of a Friday searching through our code for funny words.</p>
<p>I decided to open up by looking for traditional profanities &#8211; a commonly-accepted method of gauging the state of a project (see: <a href="http://www.vidarholen.net/contents/wordcount/">profanities in the Linux kernel, graphed over time</a> and <a href="http://www.kuro5hin.org/story/2004/2/15/71552/7795">a quick look at the Win2k source</a>).</p>
<p>Below, I provide a list of my findings, with a few examples.  Enjoy a look at the source code of a functioning product with many users!</p>
<p><em>Disclaimer: if any of the words that you are about to read will offend you, then don&#039;t read them.</em></p>
<h1>The classics</h1>
<p>I started off by looking for looking for the current English classics:</p>
<p><strong>Fuck</strong>: 6 times.<br />
Example:</p>
<pre>// If they had any unsaved changes, they're pretty much fucked, because
// this customer has apparently been modified by another screen.</pre>
<p><strong>Shit</strong>: 10</p>
<pre>// For now, I believe that all the data is updated on the fly, so saving first is not
// necessary, but maybe we should just for shits and giggles</pre>
<p><strong>Crap</strong>: 22<br />
The expletive for the more vocabulary-sensitive programmer!</p>
<pre>//!!! It was late, we were tired, so I hacked this crap together !!!</pre>
<p><!-- wut --></p>
<pre>// What the crap does this button do? - I disabled it because I think that this button
// is misleading and confusing</pre>
<p><strong>Hell</strong>: 1</p>
<pre>// If it wasn't any of the valid options, then get the hell out</pre>
<h1>Insults</h1>
<p>It&#039;s usually another programmer&#039;s fault.</p>
<p><strong>Stupid</strong>: 75<br />
The code is stupid, the users are stupid, other developers are stupid.  According to the comments, at least.</p>
<pre>// Clear out year if it is a stupid number</pre>
<p><!-- wut --></p>
<pre>// Here we tell the user they are stupid when they do stupid things
// If the user forgets to specify a table, gently remind them that it's a good idea,
// then return</pre>
<p><!-- wut --></p>
<pre>// When you minimize, it tells you you're size 0 now.  That seems stupid, ignore it</pre>
<p><!-- wut --></p>
<pre>// Die, stupid message boxes</pre>
<p><!-- wut --></p>
<pre>TRACE("Stupid quickbooks.\n");</pre>
<p><!-- wut --></p>
<pre>IQBBasePtr stupid = response-&gt;GetDetail();
TRACE("Response Type: %d\n", (int)stupid-&gt;Type-&gt;GetValue());
InterpretResponse(stupid);</pre>
<p><!-- wut --></p>
<pre>// Thigs are different (stupid) without this flag.  I don't currently support its omission</pre>
<p><strong>Dumb</strong>: 17</p>
<pre>// There was some seriously wacky code here earlier including a really dumb memory leak.
// I tested my changes and they seem to work fine, nevertheless, I wasn't able
// to fully understand what this code was attempting to do so I may have missed
// some obscure feature.</pre>
<p><strong>Retarded</strong>: 4</p>
<pre>// Because CStrings are retarded and don't do well with binary data, we have to copy the
// data in to the query in a rather inconvenient manner.</pre>
<p><strong>Idiot</strong>: 2</p>
<pre>// Else politely inform the user that they are an idiot</pre>
<p><strong>Hack</strong>: 6</p>
<pre>// Look at the first part of the label to find the operation (this is a sort of
// dirty hack so that) we don't have to change the database.</pre>
<h1>Incredulity</h1>
<p>Really?</p>
<p><strong>Why</strong>: 84<br />
The only word with a higher count than &#034;stupid&#034; &#8211; there is always more for a programmer to learn.  Or speculate on.  Or rage at.</p>
<pre>//!!! For some reason ODBC crashes at this point if you've already done a query !!!
//!!! I can't really figure out why !!!</pre>
<p><!-- wut --></p>
<pre>// Not sure why we would want to do bitwise operations...</pre>
<p><!-- wut --></p>
<pre>// WHy doesn't this exist?</pre>
<p><!-- wut --></p>
<pre>// I don't see why we'd do this</pre>
<p><!-- wut --></p>
<pre>// CRH asks: Why do we use Format() instead of "="? Can't we assume that it's slower?
// JDD Answers: what Formats?  What are you talking about?  You crazy, Charles!</pre>
<p><!-- wut --></p>
<pre>// Removed 2006.02.17 - This makes it so that negative balances show up positive.
// I am not sure sure why it was ever here.</pre>
<p><!-- wut --></p>
<pre>// This doesn't seem to work.  I'm not sure why</pre>
<p><strong>wtf</strong>: 25<br />
Often heard expressed verbally by developers reading code.</p>
<pre>catch (...)
{
	TRACE("Seriously, wtf\n");
	// sic vita est
}</pre>
<p><!-- wut --></p>
<pre>// Sort the incoming files alphabetically (windows sometimes reverses the order (wtf?)</pre>
<h1>Random</h1>
<p><strong>Ridiculous</strong>: 2<br />
When three syllables just isn&#039;t enough.</p>
<pre>// Make the quantity not have ridiculous decimal places</pre>
<p><strong>Pants</strong>: 3<br />
That&#039;s right, and all 3 times inside of a static string, too!</p>
<pre>strPCProductCode = GetInitSetting(__MASTERWND, "session", "productcode", "pants");</pre>
<p>That fourth argument is the default value, returned if there is no existing setting with that name.  So&#8230; yeah.</p>
<p><strong>lol</strong>: 3</p>
<pre>// lol xor
if ((bPaid || bUnpaid) &amp;&amp; !(bPaid &amp;&amp; bUnpaid))</pre>
<p><strong>lulz</strong>: 1</p>
<pre>// Half implemented, lulz</pre>
<h1>A few other gems</h1>
<p>Ebonics?</p>
<pre>// If shift was already depressed, fo' real, don't screw that up</pre>
<p>Obvious mental deterioration:</p>
<pre>// A grape typed this line in:
//9</pre>
<p>And&#8230; uh&#8230; this was at the bottom of a header file&#8230;</p>
<pre>// Standardized code makes him happy!
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;                        ;;
;;        ;;;;;;;;        ;;
;;       ;;      ;;       ;;
;;      ;; ;;  ;; ;;      ;;
;;      ;;        ;;      ;;
;;      ;;    ;   ;;      ;;
;;      ;;        ;;      ;;
;;      ;; ;;;;;; ;;      ;;
;;       ;;  ;;  ;;       ;;
;;        ;;;;;;;;        ;;
;;                        ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;

// Non-Standardized code makes him sad!
// And keeps him up all night, screaming
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;                        ;;
;;        ;;;;;;;;        ;;
;;       ;;;    ;;;       ;;
;;      ;; /*\/*\ ;;      ;;
;;      ;; \_/\_/ ;;      ;;
;;      ;;   ^^   ;;      ;;
;;      ;; /****\ ;;      ;;
;;      ;; \****/ ;;      ;;
;;       ;;      ;;       ;;
;;        ;;;;;;;;        ;;
;;                        ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;</pre>
<p>8-|</p>
<h1>So, yeah&#8230;</h1>
<p>Found any entertaining metadata in code?  Link me!</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduff.com/203/profanities-and-other-funny-words/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Sandwich war crimes: the Dirty Mother</title>
		<link>http://joshduff.com/200/dirty-mother</link>
		<comments>http://joshduff.com/200/dirty-mother#comments</comments>
		<pubDate>Mon, 24 May 2010 08:57:31 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[Deep thoughts]]></category>

		<guid isPermaLink="false">http://joshduff.com/?p=200</guid>
		<description><![CDATA[For those attending a university, the end of a semester is a time of much rejoicing.  During my last semesters at such an institution, I took place in an end-of-year celebratory tradition involving a ridiculous sandwich. In some issue of Maxim (November 2005?) there was an article featuring absurd sandwich-based foods.  So ridiculous were these [...]]]></description>
			<content:encoded><![CDATA[<p>For those attending a university, the end of a semester is a time of much rejoicing.  During my last semesters at such an institution, I took place in an end-of-year celebratory tradition involving a ridiculous sandwich.</p>
<p>In some issue of Maxim (November 2005?) there was an article featuring absurd sandwich-based foods.  So ridiculous were these edibles that some fraternity brothers of mine decided that the consumption of one such sandwich would make a fitting end-of-semester celebration.</p>
<p>I was unable to find the original article online, but found the ingredients <a href="http://www.ntxalliance.com/index.php?action=printpage;topic=3970.0">here</a>.  I post them here for posterity.</p>
<h2>The Dirty Mother</h2>
<blockquote><p>&#034;Use two slices of buttery Texas toast as bookends, and slap together a sausage patty, hash browns, and three generous chops of ham, separating each layer with a slice of cheddar cheese. Throw two fried eggs in the mix, and comfort them with four strips of crispy bacon. A splash of ketchup or Worcestershire sauce is your choice, but tons of S&amp;P is mandatory.&#034;</p></blockquote>
<p>We would usually go to IHOP, order the ingredients individually, and assemble the sandwiches ourselves.  The braver souls in the group would purchase fries as a side dish.</p>
<p>These memories are making me hungry.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduff.com/200/dirty-mother/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Run a query for every table in a database</title>
		<link>http://joshduff.com/189/run-a-query-for-every-table-in-a-database</link>
		<comments>http://joshduff.com/189/run-a-query-for-every-table-in-a-database#comments</comments>
		<pubDate>Wed, 17 Feb 2010 20:16:12 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://joshduff.com/?p=189</guid>
		<description><![CDATA[Ever wished you could run a dangerous query like DROP TABLE or TRUNCATE against every table in a database in a single query? No? Well, how about something like CHECK or REPAIR table, then? If you ever find yourself in the rare situation where you need to run the same query across every table in [...]]]></description>
			<content:encoded><![CDATA[<p>Ever wished you could run a dangerous query like DROP TABLE or TRUNCATE against every table in a database in a single query?</p>
<p>No?</p>
<p>Well, how about something like CHECK or REPAIR table, then?</p>
<p>If you ever find yourself in the rare situation where you need to run the same query across every table in a database, this procedure might make your life easier:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;">DELIMITER $$
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">PROCEDURE</span> <span style="color: #008000;">`p<span style="color: #008080; font-weight: bold;">_</span>run<span style="color: #008080; font-weight: bold;">_</span>for<span style="color: #008080; font-weight: bold;">_</span>each<span style="color: #008080; font-weight: bold;">_</span>table`</span><span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">IN</span> strDatabase <span style="color: #999900; font-weight: bold;">TEXT</span><span style="color: #000033;">,</span> <span style="color: #990099; font-weight: bold;">IN</span> strOperation <span style="color: #999900; font-weight: bold;">TEXT</span><span style="color: #FF00FF;">&#41;</span>
    <span style="color: #990099; font-weight: bold;">DETERMINISTIC</span>
<span style="color: #990099; font-weight: bold;">BEGIN</span>
	<span style="color: #990099; font-weight: bold;">DECLARE</span> strQuery <span style="color: #999900; font-weight: bold;">TEXT</span><span style="color: #000033;">;</span>
	<span style="color: #990099; font-weight: bold;">DECLARE</span> strTable <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">255</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
	<span style="color: #990099; font-weight: bold;">DECLARE</span> bDone <span style="color: #999900; font-weight: bold;">INT</span> <span style="color: #990099; font-weight: bold;">DEFAULT</span> <span style="color: #008080;">0</span><span style="color: #000033;">;</span>
	<span style="color: #990099; font-weight: bold;">DECLARE</span> TableCursor CURSOR FOR
		<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008000;">`TABLE<span style="color: #008080; font-weight: bold;">_</span>NAME`</span>
		<span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #008000;">`information<span style="color: #008080; font-weight: bold;">_</span>schema`</span>.<span style="color: #008000;">`TABLES`</span>
		<span style="color: #990099; font-weight: bold;">WHERE</span> <span style="color: #008000;">`TABLE<span style="color: #008080; font-weight: bold;">_</span>SCHEMA`</span> <span style="color: #CC0099;">=</span> strDatabase <span style="color: #CC0099; font-weight: bold;">AND</span> TABLE_TYPE <span style="color: #CC0099;">=</span> <span style="color: #008000;">'BASE TABLE'</span><span style="color: #000033;">;</span>
	<span style="color: #990099; font-weight: bold;">DECLARE</span> CONTINUE <span style="color: #990099; font-weight: bold;">HANDLER</span> FOR <span style="color: #CC0099; font-weight: bold;">NOT</span> FOUND <span style="color: #990099; font-weight: bold;">SET</span> bDone <span style="color: #CC0099;">=</span> <span style="color: #008080;">1</span><span style="color: #000033;">;</span>
&nbsp;
	OPEN TableCursor<span style="color: #000033;">;</span>
&nbsp;
	<span style="color: #000099;">REPEAT</span>
		FETCH TableCursor <span style="color: #990099; font-weight: bold;">INTO</span> strTable<span style="color: #000033;">;</span>
&nbsp;
		<span style="color: #990099; font-weight: bold;">SET</span> @QueryThatWasPassedIn <span style="color: #CC0099;">:=</span> <span style="color: #000099;">REPLACE</span><span style="color: #FF00FF;">&#40;</span>
			<span style="color: #000099;">REPLACE</span><span style="color: #FF00FF;">&#40;</span>strOperation<span style="color: #000033;">,</span> <span style="color: #008000;">'{?database}'</span><span style="color: #000033;">,</span> strDatabase<span style="color: #FF00FF;">&#41;</span>
			<span style="color: #000033;">,</span> <span style="color: #008000;">'{?table}'</span><span style="color: #000033;">,</span> strTable<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
		PREPARE Statement <span style="color: #990099; font-weight: bold;">FROM</span> @QueryThatWasPassedIn<span style="color: #000033;">;</span>
		EXECUTE Statement<span style="color: #000033;">;</span>
	UNTIL bDone <span style="color: #009900;">END</span> <span style="color: #000099;">REPEAT</span><span style="color: #000033;">;</span>
&nbsp;
	CLOSE TableCursor<span style="color: #000033;">;</span>
	DEALLOCATE PREPARE Statement<span style="color: #000033;">;</span>
<span style="color: #009900;">END</span>$$
DELIMITER <span style="color: #000033;">;</span></pre></div></div>

<h1>Useage</h1>
<p>The procedure takes two parameters.  The first one is the name of the database whose tables you want to run the query for.</p>
<p>The second is the query you would like to run.  The strings &#034;{?database}&#034; and &#034;{?table}&#034; will be replaced with the database and table names.</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CALL</span> p_run_for_each_table<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'databasename'</span><span style="color: #000033;">,</span> <span style="color: #008000;">'SELECT * FROM {?database}.{?table}'</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></div></div>

<h1>What queries you can run</h1>
<p>You should be able to use any queries that can be run in a prepared statement &#8211; you can find the list about two-thirds of the way down <a href="http://dev.mysql.com/doc/refman/5.1/en/sql-syntax-prepared-statements.html">this page</a>.</p>
<p>As of MySQL 5.1, you can run these queries: &#034;ALTER TABLE, CALL, COMMIT, CREATE INDEX, CREATE TABLE, DELETE, DO, DROP INDEX, DROP TABLE, INSERT, RENAME TABLE, REPLACE, SELECT, SET, UPDATE, and most SHOW statements.&#034;</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduff.com/189/run-a-query-for-every-table-in-a-database/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Logical errors in queries: DO NOT WANT</title>
		<link>http://joshduff.com/173/logical-errors-in-queries-do-not-want</link>
		<comments>http://joshduff.com/173/logical-errors-in-queries-do-not-want#comments</comments>
		<pubDate>Wed, 03 Feb 2010 03:17:58 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://joshduff.com/?p=173</guid>
		<description><![CDATA[During my career developing database-driven software (teehee, I&#039;m a professional) I&#039;ve noted that the most horrific query errors are the logical ones &#8211; queries that parse correctly, and return reasonable-looking data, but make wrong assumptions about how different parts of the query relate to each other. One particular error that I&#039;ve seen time and time [...]]]></description>
			<content:encoded><![CDATA[<p>During my career developing database-driven software (teehee, I&#039;m a professional) I&#039;ve noted that the most horrific query errors are the logical ones &#8211; queries that parse correctly, and return reasonable-looking data, but make wrong assumptions about how different parts of the query relate to each other.</p>
<p>One particular error that I&#039;ve seen time and time again (even from people who have been writing queries for a while) can occur when summarizing data from multiple tables that have a one-to-many relationship.</p>
<p>&#8230;In other words, it could occur in queries written for most database-driven software.</p>
<h2>Solution: fix the problem by writing about it!</h2>
<p>I wrote <a href="http://wikido.isoftdata.com/index.php/The_GROUPing_pitfall">a page documenting</a> the cause of the logical error, doing my best to warn people against letting it slip into their own code.</p>
<p>I attempted to write it so it would be easy to read, possibly even entertaining (a lofty goal for a manual on writing database queries, perhaps) &#8211; there is some colorful language and plenty of juvenile humor mixed with the tech-speak.</p>
<p>The doc itself is part of the wiki of the company where I work.  I don&#039;t write a ton of documentation for our developers or customers (certainly not as much as I should), but whenever I do, I get this awesome feeling of usefulness.  Oh, and pride.  Sometimes, I feel so proud, that I feel compelled to link other people (who have no relationship to my company) to what I wrote!  Ridiculous, I know.</p>
<p>Remember: if you write queries, it is your responsibility to guarantee that they return true and accurate data!</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduff.com/173/logical-errors-in-queries-do-not-want/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

