#!/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 <<EOF;
<html><head><title>$TITLE</title></head>
<frameset cols="50,50">
<frame src="$script_name/query" name="query">
<frame src="$script_name/response" name="response">
</frameset>
EOF
    ;
    exit 0;
}
#------------------------------------------

sub print_query{
    $script_name = $query->script_name;
    print "<H1>Query</H1>\n";
    print    $query->startform(-action=>"$script_name/response",-TARGET=>"response");

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

    
    print "<P> FROM: <BR> (tables)  ",
    $query->textfield(-name=>'tables', 
			-default=>'bodyt',
			-size=>30,
			-maxlength=>120);
    
    print "<P> WHERE: <BR> (condition) ",
    $query->textfield(-name=>'condition', 
			-default=>'bidt>0 and bidt<12',
			-size=>50,
			-maxlength=>120);
    
    print "<P> ORDER BY: <BR> (fields) ",
    $query->textfield(-name=>'order', 
			-default=>'',
			-size=>30,
			-maxlength=>120);


    print "<BR><BR>",$query->submit (' Submit query ');
    print $query->endform;
 print qq{<P><A href="http://barley.itc.nl/VRML/START7.html" TARGET= "vrml" >back</A>};

}
#--------------------------------------------
sub print_response {
    print "<H1>Result</H1>\n";
    unless ($query->param) {
	print "<b>No query submitted yet.</b>";
	return;
    }


#Query database--1------------------------------------------------

$fields = $query->param('fields');
$tables = $query->param('tables');
$condition = $query->param('condition');
$order=$query->param('order');

print "<P><B><FONT COLOR=red>SELECT $fields FROM $tables WHERE $condition ORDER BY $order</FONT></B></P>";


$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 "<BR>";
  $num++;
}

$sth->finish;

$dbh->disconnect;

 

}


