10.07.2015 Views

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

68CHAPTER 3 ■ DATABASES AND PERLmy $username = "dbuser";my $password = "dbpassword";my $dsn = "dbi:mysql:mysql:192.168.1.10";my $dbh = DBI->connect($dsn,$username,$password)or die "Cannot connect <strong>to</strong> database: $DBI::errstr";my $sth = $dbh->prepare("SELECT host,user FROM mysql.user");$sth->execute()or die "Cannot execute sth: $DBI::errstr";print header,start_html('MySQL Hosts and Users'),table({-border=>1}),Tr({-align=>'CENTER',-valign=>'TOP'},[th(['User','Host'])]);while (my ($hostname,$username) = $sth->fetchrow_array()) {if ($hostname eq "") {$hostname = "undef";}print Tr({-align=>'CENTER',-valign=>'TOP'},[td(["$username","$hostname"])]);}print end_html;$dbh->disconnect();Running this program shows that the data has been placed in<strong>to</strong> an HTML table, as shownin Figure 3-2.In Listing 3-9, first the table is created using CGI.pm’s table() method:print header,start_html('MySQL Hosts and Users'),table({-border=>1}),Tr({-align=>'CENTER',-valign=>'TOP'},[th(['User','Host'])]);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!