Perl script makes MySQL query; outputs to JSON
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.
This can be seen in action on this page: http://www.ruddwire.com/friendly-data/naics-explorer/.
#!/usr/bin/perl use strict; use DBI(); use CGI qw(:standard); my $dbh; my $sth; my $mySQL; my $result = '({"failure":1})'; #QUERY PARAMS my $strLn; my $classification; my $getDef; my $getIndus; my $naicsMatch = ""; my $countMatch = ""; my @subClasses; my $ref; my $table = ""; my $tableRow; sub scrubInputText { my($it) = @_; my $shellChars = q-[&;`'\\\"\|\*\?\~<>\^\(\)\[\]\{\}\$\\n\\r]-; #` $it =~ s/$shellChars//g; $it =~ s/^\s+|\s+$//g; return $it; } my $query = new CGI; if($query->param()){ $strLn = &scrubInputText($query->param('strLn')); $classification = &scrubInputText($query->param('cls')); $getDef = &scrubInputText($query->param('getDef')); $getIndus = &scrubInputText($query->param('getIndus')); ###HANDLE GROUPED SECTOR CODES, IE: 31-33 if($classification =~ /-/){ @subClasses = split("-",$classification); $naicsMatch = "AND ("; for(my $subClass = $subClasses[0]; $subClass <= $subClasses[1]; $subClass++){ $naicsMatch .= "code LIKE '${subClass}%' OR "; $countMatch .= "code LIKE '${subClass}%' OR "; } $countMatch =~ s/ OR $//; $naicsMatch =~ s/ OR $//; $naicsMatch .= ")"; }else{ $naicsMatch = "AND code LIKE '${classification}%'"; } if($strLn != 2){$countMatch = "code LIKE Concat(naics2007.code,'%')";} ### Connect to the database. my $dbh = DBI->connect("DBI:mysql:database=***DBNAME***;host=localhost", "***USERNAME***", "***PASSWORD***", {'RaiseError' => 1}); if($getDef){ $mySQL = "SELECT definition FROM naics2007definitions WHERE code='${classification}'"; }elsif($getIndus){ ### GET FINAL INDUSTRY NAME $mySQL = "SELECT code,industry FROM naics2007IndustryIndex WHERE code LIKE '${classification}' ORDER BY industry"; }else{ ### GET INDUSTRY CLASSIFICATIONS $mySQL = "SELECT code,title, ". "(SELECT COUNT(code) FROM naics2007IndustryIndex WHERE ". "CASE naics2007.code ". "WHEN '31-33' THEN (code LIKE '31%' OR code LIKE '32%' OR code LIKE '33%') ". "WHEN '44-45' THEN (code LIKE '44%' OR code LIKE '45%') ". "WHEN '48-49' THEN (code LIKE '48%' OR code LIKE '49%') ". "ELSE code LIKE Concat(naics2007.code,'%') END) AS c". " FROM naics2007 WHERE (CHAR_LENGTH(code) = $strLn OR code LIKE '__-__')".$naicsMatch; } $sth = $dbh->prepare($mySQL); $sth->execute(); while ($ref = $sth->fetchrow_hashref()) { while ((my $key, my $value) = each(%$ref)){ $value =~ s/"/\\"/g; $tableRow .= "\"${value}\","; } $tableRow =~ s/,$//; $tableRow = "[${tableRow}],"; $table .= $tableRow; $tableRow = ""; } $table =~ s/,$//; $result = '{"failure":0,"result":['.$table.']}'; }### END IF QUERY print "Content-type: application/json; charset=iso-8859-1\n\n"; print $result; exit(0);