#! c:/perl/bin/perl.exe # Show a MySQL Table # Table and DB name specified via Querystring. # April 2007, by Vijay Anand use strict; use CGI qw/:standard *table start_ul/; use DBI; use CGI::Carp qw(warningsToBrowser fatalsToBrowser); my $db = param ("DB") || "hardware"; my $table = param ("TABLE") || "subnet"; my $row_limit = param("ROWLIMIT") || 400; my $dbh = DBI->connect("DBI:mysql:database=$db;host=localhost","root","**YOUR PASSWORD**") or die "Could not connect to database $db (credentials?)\n"; my $returnFields=param("return") || "*"; #parse through criteria parameters my @allcriteria=(); my @fieldnames; ##my @allparams=param(); foreach my $param ( param() ) { next unless $param=~/criteria_condition_(.*)/; push @fieldnames, $1; next if param($param) eq "0"; # Ignore zero criteria next unless param("criteria_value_$1"); #ignore blank criteria push @allcriteria, $1." ".param($param)." ".param("criteria_value_$1"); } my $queryCriteria=join(" ".param("criteria_logic")." ",@allcriteria); my $sqlstatement="select $returnFields from $table"; if ($queryCriteria) { $sqlstatement.=" where ($queryCriteria)" } param("criteria_grouping") and $sqlstatement .= " " . param("criteria_grouping"); Print_Header_And_Styles(); my $sth = $dbh->prepare($sqlstatement); $sth->execute(); { my @fieldnames_new = Display_Rows(); @fieldnames=@fieldnames_new if @fieldnames_new; } $sth->finish(); $dbh->disconnect; Print_criteria_customization_form(@fieldnames); print end_html; #------------------------------------------------ sub Display_Rows{ my $row = 0; my @fieldnames; my @ToggleClass = ( {-class=>"coloredrow"}, {-class=>"treedetailbg"}); print start_table({-border=>undef, -align=>'LEFT'}), # , -width=>'100%' Tr( td({-bgcolor=>'LIGHTGREEN'}, a({-href=>url(-base=>1) }, b("HOME")) ), td( $sth->rows . " rows available"), td("DB=$db"), td("Table=$table"), td($queryCriteria || "*"), td ({-bgcolor=>'LIGHTGREEN' }," ") ), end_table, "\n", br({clear=>'left'}) # Re-flow page to the LEFT. #p({-align=>'LEFT'},"  \n"), # This piece is required, to fix rendering problems ; return if $sth->rows < 1; #SOMETIMES, WE GET NEGATIVE ONE print start_table({-border=>1, -cellpadding=>"1", # , -width=>'100%' -cellspacing=>"1", -align=>'LEFT'}); while( my $rec = $sth->fetchrow_hashref()) { # Print header, if this is the first row ++$row == 1 and print Tr( map {my $h=$_;$h=~s/_/ /g; td({-class=>"treedetailheader"} ,b($h))} @fieldnames = keys(%$rec) ), "\n"; last if ($row > $row_limit); # Print field data.. print Tr( $ToggleClass[$row % 2] , map {td( escapeHTML($rec->{$_}) )} keys %$rec ), "\n"; } print end_table,"\n"; #Does not help: print p({-align=>'LEFT'},'  '); # This piece is required, to fix rendering problems $row > $row_limit and print h4 ("--- Truncated to $row_limit Rows ---"); return @fieldnames; } #------------------------------------------------ sub Print_criteria_customization_form{ my @fieldNames = @_; print br({clear=>'left'}) ;# Re-flow page to the LEFT. my $tableHTML="
\n"; $tableHTML .= "\n\n"; foreach my $fieldName (@fieldNames) { $tableHTML.=""; $tableHTML.="\n"; } $tableHTML.="\n"; $tableHTML.="
" . "Customize the table view using the fields below.". "
You must put single quotes around text values.". "
Click Apply with all set to IGNORE to view whole table.
$fieldName"; $tableHTML.="
Combine criteria with
" . "\n". "\n". "". "
". ""; print $tableHTML; } #------------------------------------------------ sub Print_Header_And_Styles{ print header; print start_html("CGI DBI $db $table"); print <<'STYLESHEET'; STYLESHEET } #------------------------------------------------