Creating a Web Page Integrated with SQL Data : select « Database « Perl






Creating a Web Page Integrated with SQL Data

   


#!/usr/bin/perl

use DBI;
use strict;
use CGI qw/:standard/;

my $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 to database: $DBI::errstr";

my $hosttolookup = "%";

my $sth = $dbh->prepare("SELECT host FROM mysql.user WHERE host LIKE ?");

$sth->execute($hosttolookup) or die "Cannot execute sth: $DBI::errstr";

my @mysqlhosts;
while (my $hostname = $sth->fetchrow_array()) {
    if ($hostname =~ /%/) {
        push (@mysqlhosts,$hostname);
    }
}

print header,
      start_html('MySQL Hosts Using Wildcards');

my $count = @mysqlhosts;
if ($count == 0) {
    print p("No Hosts Using Wildcards");
}
else {
    while (<@mysqlhosts>) {
        print p("Host Wildcard: $_");
    }
}
print end_html;

$dbh->disconnect();

   
    
    
  








Related examples in the same category

1.Select, Execute, and Dump the Results
2.Select, Execute, and Fetch a Row as a Hash
3.Select, Execute, and Fetch a Row as an Array
4.Simple query
5.The First SQL Query
6.Using a Placeholder
7.Creating an HTML Table