#!/usr/local/bin/perl -w # Select 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 "

SELECT:
(fields) ", $query->textfield(-name=>'fields', -default=>'*', -size=>30, -maxlength=>120); print "

FROM:
(tables) ", $query->textfield(-name=>'tables', -default=>'bodyt', -size=>30, -maxlength=>120); print "

WHERE:
(condition) ", $query->textfield(-name=>'condition', -default=>'bidt>0 and bidt<12', -size=>50, -maxlength=>120); print "

ORDER BY:
(fields) ", $query->textfield(-name=>'order', -default=>'', -size=>30, -maxlength=>120); print "

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

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

Result

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

SELECT $fields FROM $tables WHERE $condition ORDER BY $order

"; $dbh = DBI->connect ("DBI:mysql:$database:$hostname", $user,$password); if ($order ne '') { $sth = $dbh->prepare("select $fields from $tables where $condition order by $order"); } $sth = $dbh->prepare("select $fields from $tables where $condition "); $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; }