DATABASE :: Perl MySQL Management CGI
2000
This code sample is part of a generic Perl CGI script that was written to manage any mySQL database via the web. It uses both the CGI and Msql Perl modules.
#!/usr/local/bin/perl -w
# MySQL Database Management in Perl
use CGI;
use CGI qw/escape unescape/;
use Msql;
###################################################################
# VARS
$DBNAME = "mydatabase"; # Database to work with
$script = $ENV{'SCRIPT_NAME'};
$style=<<END;
BODY,P,TD,UL,LI,CENTER {
font-family: Helvetica, Arial, sans-serif;
font-size: 10pt;
}
.color {
background: DDEEFF;
}
END
###################################################################
# Connect and open the database
$dbh = Msql->connect;
$dbh->selectdb( $DBNAME );
$query = new CGI;
$command = $query->param('command');
$table = $query->param('table');
$tableandorder = $query->param('tableandorder');
$columnselect = $query->param('columnselect');
$columnselect2 = $query->param('columnselect2');
$recordselect = $query->param('recordselect');
$recordselect =~ s/\'/\\'/gi;
# Now that we have all the parameters, clear out the parameter values
$query->delete_all();
$| = 1; # unbuffer stdout
...
###################################################################
# Print out header/start of html
sub doHeader {
print $query->header;
print $query->start_html( -title=>'Database',
-BGCOLOR=>'#CCCCCC',
-style=>$style);
}
###################################################################
# Shows tables avail in database
sub showTables {
print $query->startform(post,$script);
print "<P><B>Select a table to view/edit:</B></P>\n\n";
@tables = $dbh->listtables;
%tableandorder = ();
foreach $table (@tables) {
$sqlquery = "SELECT * FROM $table";
$RS = $dbh->query( $sqlquery ) || &error($Msql::db_errstr);
@list = $RS->name;
$thisvalue = $table . '~~~~' . $list[0];
$tableandorder{$thisvalue} = $table;
}
print $query->popup_menu(-name=>'tableandorder',
-values=>\%tableandorder);
print $query->hidden(-name=>'command',
-default=>'showAll');
print $query->submit(-value=>'SUBMIT');
print $query->endform;
print $query->end_html;
exit(0);
}