next up previous contents index
Next: MySQL and CGI Up: Perl DBI Previous: Installing Msql-Mysql-modules   Contents   Index

Testing Perl DBI

You can create a Perl program that will read messages from the table that we created. Here is some example program called dbitest.pl that will do just this:

#!/usr/bin/perl

use DBI;

$dsn = "DBI:mysql:bob:localhost"; #data source name
$user_name = "bob"; # user name
$password = ""; # passwords are either blank or existing

$dbh = DBI->connect($dsn, $user_name, $password, { RaiseError => 1});

$count = 0;
$infoget = "SELECT * FROM messages";
$sth = $dbh->prepare ($infoget);
$sth->execute();
while(@info = $sth->fetchrow_array()) {
    ($num, $sender, $message) = @info;
    print "$num ($sender) - $message\n";
    $count++;
}

print "$count Message(s)\n";

exit;

To run and test this program first change the permissions to 755 (as explained in Section 4.6) then run the program:

bash$ ./dbitest.pl
1 (admin) - Hello Bob, how are you?
1 Message(s)



Joseph Colton 2002-09-24