<?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>RuddWire &#187; mySQL</title>
	<atom:link href="http://www.ruddwire.com/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ruddwire.com</link>
	<description>Ruddwire.com: food, book, theatre reviews, data presentation projects, code snippets, millisecond date calculators</description>
	<lastBuildDate>Tue, 27 Dec 2011 02:19:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Perl script make MySQL query, binds variables, outputs JSON</title>
		<link>http://www.ruddwire.com/1049/tech/perl-script-make-mysql-query-binds-variables-outputs-json/</link>
		<comments>http://www.ruddwire.com/1049/tech/perl-script-make-mysql-query-binds-variables-outputs-json/#comments</comments>
		<pubDate>Sat, 17 Jul 2010 00:40:11 +0000</pubDate>
		<dc:creator>Colin</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[tech]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[mySQL]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://www.ruddwire.com/?p=1049</guid>
		<description><![CDATA[An example of a Perl script that is called by an ajax request.  The script takes in just one parameter, opens a MySQL DB connection, binds the parameter to the query, and sends the query.  The result is then formatted into JSON and returned to the browser.
This can be seen in action on [...]]]></description>
			<content:encoded><![CDATA[<p>An example of a Perl script that is called by an ajax request.  The script takes in just one parameter, opens a MySQL DB connection, binds the parameter to the query, and sends the query.  The result is then formatted into JSON and returned to the browser.</p>
<p>This can be seen in action on this page: <a href="http://www.ruddwire.com/friendly-data/naics-explorer/">http://www.ruddwire.com/friendly-data/naics-explorer/</a>, in the NAICS search box.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">&nbsp;
<span style="color: #666666; font-style: italic;">#!/usr/bin/perl</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> DBI<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> CGI <span style="color: #000066;">qw</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">:</span>standard<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sth</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$mySQL</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$result</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'({&quot;failure&quot;:1})'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#QUERY PARAMS</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$q</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$ref</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$table</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$tableRow</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$query</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CGI<span style="color: #339933;">;</span>
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">param</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>
&nbsp;
&nbsp;
    <span style="color: #0000ff;">$q</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">param</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'q'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$q</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">s/\s+/%/</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;"># REPLACE SPACES WITH WILD CARD CHARACTER, CAUSES TO SEARCH FOR ALL WORDS, NOT JUST PHRASE</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">### Connect to the database.</span>
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dbh</span> <span style="color: #339933;">=</span> DBI<span style="color: #339933;">-&gt;</span><span style="color: #006600;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;DBI:mysql:database=***DBNAME***;host=localhost&quot;</span><span style="color: #339933;">,</span>
                         <span style="color: #ff0000;">&quot;***USERNAME***&quot;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;***PASSWORD***&quot;</span><span style="color: #339933;">,</span>
                         <span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'RaiseError'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    	<span style="color: #666666; font-style: italic;">### GET INDUSTRY NAME AND CODE</span>
    	<span style="color: #0000ff;">$mySQL</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;SELECT code,industry FROM naics2007IndustryIndex WHERE industry LIKE ? ORDER BY industry&quot;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
     <span style="color: #0000ff;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$mySQL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%&quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$</span><span style="color: #009900;">&#123;</span><span style="color: #000066;">q</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">.</span><span style="color: #ff0000;">&quot;%&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$ref</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">fetchrow_hashref</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>
&nbsp;
		  <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$key</span><span style="color: #339933;">,</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #000066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">%$ref</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		  	  <span style="color: #0000ff;">$value</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">s/&quot;/\\&quot;/g</span><span style="color: #339933;">;</span>
			  <span style="color: #0000ff;">$tableRow</span> <span style="color: #339933;">.=</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>${value}<span style="color: #000099; font-weight: bold;">\&quot;</span>,&quot;</span><span style="color: #339933;">;</span>
		  <span style="color: #009900;">&#125;</span>
&nbsp;
		  <span style="color: #0000ff;">$tableRow</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">s/,$//</span><span style="color: #339933;">;</span>
		  <span style="color: #0000ff;">$tableRow</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;[${tableRow}],&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		  <span style="color: #0000ff;">$table</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">$tableRow</span><span style="color: #339933;">;</span>
		  <span style="color: #0000ff;">$tableRow</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #0000ff;">$table</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">s/,$//</span><span style="color: #339933;">;</span>
&nbsp;
	  	<span style="color: #0000ff;">$result</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'{&quot;failure&quot;:0,&quot;result&quot;:['</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$table</span><span style="color: #339933;">.</span><span style="color: #ff0000;">']}'</span><span style="color: #339933;">;</span>         
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #666666; font-style: italic;">### END IF QUERY</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Content-type: text/javascript; charset=iso-8859-1<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066;">print</span> <span style="color: #0000ff;">$result</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.ruddwire.com/1049/tech/perl-script-make-mysql-query-binds-variables-outputs-json/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Perl script makes MySQL query; outputs to JSON</title>
		<link>http://www.ruddwire.com/1025/tech/perl-script-makes-mysql-query-outputs-to-json/</link>
		<comments>http://www.ruddwire.com/1025/tech/perl-script-makes-mysql-query-outputs-to-json/#comments</comments>
		<pubDate>Mon, 21 Jun 2010 01:37:34 +0000</pubDate>
		<dc:creator>Colin</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[tech]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[mySQL]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://www.ruddwire.com/?p=1025</guid>
		<description><![CDATA[<p>An example of a perl script that is called by an ajax request.  The script takes in a few parameters, opens a mySQL DB connection, and then makes a rather tortuous query.  The result is then formatted in to JSON and returned to the browser.</p>]]></description>
			<content:encoded><![CDATA[<p>An example of a Perl script that is called by an ajax request.  The script takes in a few parameters, opens a MySQL DB connection, and then makes a rather tortuous query.  The result is then formatted into JSON and returned to the browser.</p>
<p>This can be seen in action on this page: <a href="http://www.ruddwire.com/friendly-data/naics-explorer/">http://www.ruddwire.com/friendly-data/naics-explorer/</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> DBI<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> CGI <span style="color: #000066;">qw</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">:</span>standard<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sth</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$mySQL</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$result</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'({&quot;failure&quot;:1})'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#QUERY PARAMS</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$strLn</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$classification</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$getDef</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$getIndus</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$naicsMatch</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$countMatch</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@subClasses</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$ref</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$table</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$tableRow</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> scrubInputText <span style="color: #009900;">&#123;</span>
&nbsp;
   <span style="color: #b1b100;">my</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$it</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$shellChars</span> <span style="color: #339933;">=</span> q<span style="color: #339933;">-</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">&amp;;</span><span style="color: #ff0000;">`'<span style="color: #000099; font-weight: bold;">\\</span><span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\|</span><span style="color: #000099; font-weight: bold;">\*</span><span style="color: #000099; font-weight: bold;">\?</span><span style="color: #000099; font-weight: bold;">\~</span>&lt;&gt;<span style="color: #000099; font-weight: bold;">\^</span><span style="color: #000099; font-weight: bold;">\(</span><span style="color: #000099; font-weight: bold;">\)</span><span style="color: #000099; font-weight: bold;">\[</span><span style="color: #000099; font-weight: bold;">\]</span><span style="color: #000099; font-weight: bold;">\{</span><span style="color: #000099; font-weight: bold;">\}</span><span style="color: #000099; font-weight: bold;">\$</span><span style="color: #000099; font-weight: bold;">\\</span>n<span style="color: #000099; font-weight: bold;">\\</span>r]-;    #`</span>
&nbsp;
    <span style="color: #0000ff;">$it</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">s/$shellChars//g</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$it</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">s/^\s+|\s+$//g</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000066;">return</span> <span style="color: #0000ff;">$it</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$query</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CGI<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">param</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>
&nbsp;
    <span style="color: #0000ff;">$strLn</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&amp;scrubInputText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">param</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'strLn'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$classification</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&amp;scrubInputText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">param</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'cls'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$getDef</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&amp;scrubInputText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">param</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'getDef'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$getIndus</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&amp;scrubInputText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">param</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'getIndus'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">###HANDLE GROUPED SECTOR CODES, IE: 31-33</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$classification</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/-/</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    	<span style="color: #0000ff;">@subClasses</span> <span style="color: #339933;">=</span> <span style="color: #000066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;-&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$classification</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #0000ff;">$naicsMatch</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;AND (&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$subClass</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$subClasses</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: #0000ff;">$subClass</span> <span style="color: #339933;">&lt;=</span> <span style="color: #0000ff;">$subClasses</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$subClass</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    		<span style="color: #0000ff;">$naicsMatch</span> <span style="color: #339933;">.=</span> <span style="color: #ff0000;">&quot;code LIKE '${subClass}%' OR &quot;</span><span style="color: #339933;">;</span>
    		<span style="color: #0000ff;">$countMatch</span> <span style="color: #339933;">.=</span> <span style="color: #ff0000;">&quot;code LIKE '${subClass}%' OR &quot;</span><span style="color: #339933;">;</span>
    	<span style="color: #009900;">&#125;</span>
    	<span style="color: #0000ff;">$countMatch</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">s/ OR $//</span><span style="color: #339933;">;</span>
    	<span style="color: #0000ff;">$naicsMatch</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">s/ OR $//</span><span style="color: #339933;">;</span>
    	<span style="color: #0000ff;">$naicsMatch</span> <span style="color: #339933;">.=</span> <span style="color: #ff0000;">&quot;)&quot;</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: #0000ff;">$naicsMatch</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;AND code LIKE '${classification}%'&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$strLn</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$countMatch</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;code LIKE Concat(naics2007.code,'%')&quot;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">### Connect to the database.</span>
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dbh</span> <span style="color: #339933;">=</span> DBI<span style="color: #339933;">-&gt;</span><span style="color: #006600;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;DBI:mysql:database=***DBNAME***;host=localhost&quot;</span><span style="color: #339933;">,</span>
                         <span style="color: #ff0000;">&quot;***USERNAME***&quot;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;***PASSWORD***&quot;</span><span style="color: #339933;">,</span>
                         <span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'RaiseError'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</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: #0000ff;">$getDef</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    	<span style="color: #0000ff;">$mySQL</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;SELECT definition FROM naics2007definitions WHERE code='${classification}'&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">elsif</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$getIndus</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    	<span style="color: #666666; font-style: italic;">### GET FINAL INDUSTRY NAME</span>
    	<span style="color: #0000ff;">$mySQL</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;SELECT code,industry FROM naics2007IndustryIndex WHERE code LIKE '${classification}' ORDER BY industry&quot;</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: #666666; font-style: italic;">### GET INDUSTRY CLASSIFICATIONS</span>
&nbsp;
&nbsp;
 		<span style="color: #0000ff;">$mySQL</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;SELECT code,title, &quot;</span><span style="color: #339933;">.</span>
     	<span style="color: #ff0000;">&quot;(SELECT COUNT(code) FROM naics2007IndustryIndex WHERE &quot;</span><span style="color: #339933;">.</span>
     	<span style="color: #ff0000;">&quot;CASE naics2007.code &quot;</span><span style="color: #339933;">.</span>
     	<span style="color: #ff0000;">&quot;WHEN '31-33' THEN (code LIKE '31%' OR code LIKE '32%' OR code LIKE '33%') &quot;</span><span style="color: #339933;">.</span>
     	<span style="color: #ff0000;">&quot;WHEN '44-45' THEN (code LIKE '44%' OR code LIKE '45%') &quot;</span><span style="color: #339933;">.</span>
     	<span style="color: #ff0000;">&quot;WHEN '48-49' THEN (code LIKE '48%' OR code LIKE '49%') &quot;</span><span style="color: #339933;">.</span>
     	<span style="color: #ff0000;">&quot;ELSE code LIKE Concat(naics2007.code,'%') END) AS c&quot;</span><span style="color: #339933;">.</span>
     	<span style="color: #ff0000;">&quot; FROM naics2007 WHERE (CHAR_LENGTH(code) = $strLn OR code LIKE '__-__')&quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$naicsMatch</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
     <span style="color: #0000ff;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$mySQL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$ref</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">fetchrow_hashref</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>
&nbsp;
		  <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$key</span><span style="color: #339933;">,</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #000066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">%$ref</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		  	  <span style="color: #0000ff;">$value</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">s/&quot;/\\&quot;/g</span><span style="color: #339933;">;</span>
			  <span style="color: #0000ff;">$tableRow</span> <span style="color: #339933;">.=</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>${value}<span style="color: #000099; font-weight: bold;">\&quot;</span>,&quot;</span><span style="color: #339933;">;</span>
		  <span style="color: #009900;">&#125;</span>
&nbsp;
		  <span style="color: #0000ff;">$tableRow</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">s/,$//</span><span style="color: #339933;">;</span>
		  <span style="color: #0000ff;">$tableRow</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;[${tableRow}],&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		  <span style="color: #0000ff;">$table</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">$tableRow</span><span style="color: #339933;">;</span>
		  <span style="color: #0000ff;">$tableRow</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #0000ff;">$table</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">s/,$//</span><span style="color: #339933;">;</span>
&nbsp;
	  	<span style="color: #0000ff;">$result</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'{&quot;failure&quot;:0,&quot;result&quot;:['</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$table</span><span style="color: #339933;">.</span><span style="color: #ff0000;">']}'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #666666; font-style: italic;">### END IF QUERY</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Content-type: application/json; charset=iso-8859-1<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066;">print</span> <span style="color: #0000ff;">$result</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.ruddwire.com/1025/tech/perl-script-makes-mysql-query-outputs-to-json/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Handy MySQL Commands</title>
		<link>http://www.ruddwire.com/991/tech/handy-mysql-commands/</link>
		<comments>http://www.ruddwire.com/991/tech/handy-mysql-commands/#comments</comments>
		<pubDate>Sun, 25 Apr 2010 22:30:20 +0000</pubDate>
		<dc:creator>Colin</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[tech]]></category>
		<category><![CDATA[mySQL]]></category>

		<guid isPermaLink="false">http://www.ruddwire.com/?p=991</guid>
		<description><![CDATA[To reset an auto_increment field to 1:
ALTER TABLE tbl_name AUTO_INCREMENT = 1;

To load a local csv file into an empty existing table:
LOAD DATA LOCAL INFILE &#8216;/completePathToLocalData.csv&#8217; INTO TABLE tableName FIELDS TERMINATED BY &#8216;,&#8217; ENCLOSED BY &#8216;&#8221;&#8216; LINES TERMINATED BY &#8216;\n&#8217; IGNORE 1 LINES;
This assumes you&#8217;ve already created your empty table with the same number of [...]]]></description>
			<content:encoded><![CDATA[<div class="mtb">To reset an auto_increment field to 1:</p>
<div class="code">ALTER TABLE tbl_name AUTO_INCREMENT = 1;</div>
</div>
<div class="mtb">To load a local csv file into an empty existing table:</p>
<div class="code">LOAD DATA LOCAL INFILE &#8216;/completePathToLocalData.csv&#8217; INTO TABLE tableName FIELDS TERMINATED BY &#8216;,&#8217; ENCLOSED BY &#8216;&#8221;&#8216; LINES TERMINATED BY &#8216;\n&#8217; IGNORE 1 LINES;</div>
<p>This assumes you&#8217;ve already created your empty table with the same number of fields that your csv file has.</p></div>
<p><a name="js"></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ruddwire.com/991/tech/handy-mysql-commands/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

