use CGI; my $q = CGI->new(); print $q->header(); my $db = Your::Module::GetYourDataBaseHandle(); my $html = ''; my $execstr = $q->param("sqlquery"); $html .= $q->start_multipart_form( "POST", $ENV{script_name}) . "\n" . $q->hidden("displaytype") . "\n" . $q->hidden("node_id", getId($NODE)) . "\n" . "SQL Query:
\n" . $q->textarea( "sqlquery", $execstr, 8, 60 ) . "
\n" . $q->submit('execsql', 'Execute') . "\n" . $q->end_form(); print $html; $html = ''; if( $execstr ) { my $cursor = $db->prepare($execstr); my $count = eval { $cursor->execute() }; if( ! $count ) { $html= "Execute failed: $DBI::errstr\n"; } elsif( ! $cursor->{NUM_OF_FIELDS} ) { # not a select statement $html= "$count rows affected.\n"; } else { my $ROW; while( $ROW = $cursor->fetchrow_hashref() ) { if( $html eq "" ) { $html = "$count rows:\n\n"; $html .= " \n"; foreach ( @{$cursor->{NAME}} ) { if( ! defined $_ ) { $_= ""; } elsif( "" eq $_ ) { $_= " "; } else { $_= $q->escapeHTML($_); } $html .= qq[ \n]; } $html .= " \n"; } $html .= " \n"; my( $k, $v ); foreach ( @{$cursor->{NAME}} ) { #(keys %$ROW) $k = $_; $v = $$ROW{$_}; if( ! defined $v ) { $v= ""; } elsif( "" eq $v ) { $v= " "; } else { $v= $q->escapeHTML($v); $v =~ s# #  #g; $v =~ s#\n#
\n#g; } $html .= " \n"; } $html .= " \n"; } $cursor->finish(); if( $html eq "" ) { $html .= "Zero rows returned.\n"; } else { $html .= "
] . qq[$_
$v
\n" } } } print $html;