Perl Source Code: dbread.pl
#!/usr/bin/perl
use CGI qw(:standard);
use DBI;
use DBD::mysql;
use common;
use connect;
#----------------------------------------------------------
# The Web Language Project
# Mark Brautigam
# May-June 2015
# http://www.mixed-up.com/markb/
#----------------------------------------------------------
myUtils::headers1();
myUtils::headers2();
print myUtils::sidebar ("perl", "dbread", "pl");
print <<middle;
<div id="content">
<h2>Perl: Display MySQL Database Contents</h2>
<table class='results db'>
<tr><th>Park</th><th>City</th><th>State</th><th>Geo</th><th>Edit</th></tr>
middle
# Get all the entries from the database
#
$dbh = connect::my_connect();
my $sql = "SELECT * FROM parks";
my $sth = $dbh->prepare ($sql);
$sth->execute();
# Write each entry to a row in an HTML table
#
while (my $row = $sth->fetchrow_hashref) {
print "<tr><td>$row->{'site'}</td><td>$row->{'city'}</td><td>$row->{'state'}</td>";
$lat = $row->{'latitude'};
$lon = $row->{'longitude'};
printf ("<td><a target='_blank' href='https://www.google.com/maps/preview/@%f,%f,12z'>%.2f, %.2f</a></td>\n",
$lat, $lon, $lat, $lon);
print "<td><a href='dbedit.pl?id=$row->{'ID'}'><img src='../images/pencilc.svg' /></a></td>\n";
print "</tr>\n";
}
$dbh->disconnect();
print <<foot;
</table>
<p><a href='dbwrite.pl'>Write to this database table »</a></p>
<p><a href='source.pl?f=7'>Show Perl source code »</a>
</div>
foot
myUtils::footers();