#!/usr/local/bin/perl -w # Free query use DBI; use CGI; $database = '***'; $hostname = '***'; $user = '***'; $password = '***'; $query = new CGI; print $query->header; $TITLE="Query Form"; $path_info = $query->path_info; # If no path information is provided, then we create # a side-by-side frame set if (!$path_info) { &print_frameset; exit 0; } #Start HTML page &print_html_header; &print_query if $path_info=~/query/; &print_response if $path_info=~/response/; &print_end; exit; #---------------------------------------- # Subroutines #----------------------------------------- sub print_html_header { print $query->start_html($TITLE); } #----------------------------------------- sub print_end { print $query->end_html; } #----------------------------------------- sub print_frameset { $script_name = $query->script_name; print <$TITLE EOF ; exit 0; } #------------------------------------------ sub print_query{ $script_name = $query->script_name; print "

Query

\n"; print $query->startform(-action=>"$script_name/response",-TARGET=>"response"); print "

SQL QUERY:
(command) ", $query->textfield(-name=>'command', -default=>'show tables', -size=>50, -maxlength=>200); print "

",$query->submit (' Submit query '); print $query->endform; print qq{

back

}; print "


permitted commands:

CREATE TABLE, SELECT, JOIN, INSERT, LOAD DATA INFILE, SHOW, EXPLAIN

"; print qq{

check the syntax with the documentation PDF or on-line version

}; } #-------------------------------------------- sub print_response { print "

Result

\n"; unless ($query->param) { print "No query submitted yet."; return; } #Query database------------------------------------------ $command = $query->param('command'); print "

$command

"; $dbh = DBI->connect ("DBI:mysql:$database:$hostname", $user,$password); if ($command eq '') { print "

No command submitted yet

"; $dbh->disconnect; return; } $sth = $dbh->prepare("$command"); $sth->execute; $num=0; while (@field = $sth->fetchrow) { $all[$num]=[@field]; $num_rec=@field; for ($i=0;$i<$num_rec; $i++) { print "$all[$num]->[$i] "; } print "
"; $num++; } $sth->finish; $dbh->disconnect; }