#! /usr/bin/perl
# A perl program to address mysql database.
# RAW 12/12/99

    use DBI;

    $db='rnews';
    $host='localhost';
    $user='mysql_user';
    $password='mysql_password';
    $table='jpg_rec';


  $dbh = DBI->connect("DBI:mysql:database=$db;host=$host", $user, $password, {RaiseError => 1});

# INSERT SQL command
#       Once you have connected to a database, you can can execute
#       SQL statements with:

#         my $query = sprintf("INSERT INTO jpg_rex VALUES ('my.group.mysql',123987,'mysqlfilename',now())");
#         $dbh->do($query);


# query and print a table.
         my $sth = $dbh->prepare("SELECT * FROM $table");
         if (!$sth) {
             die "Error:" . $dbh->errstr . "\n";
         }
         if (!$sth->execute) {
             die "Error:" . $sth->errstr . "\n";
         }
         my $names = $sth->{'NAME'};
         my $numFields = $sth->{'NUM_OF_FIELDS'};
         for (my $i = 0;  $i < $numFields;  $i++) {
             printf("%s%s", $$names[$i], $numFields-$i-1 ? "," : "\n");
         }
         while (my $ref = $sth->fetchrow_arrayref) {
             for (my $i = 0;  $i < $numFields;  $i++) {
                 printf("%s%s", $$ref[$i], $numFields-$i-1 ? "," : "\n");
             }
         }

         $sth->finish();
         $dbh->disconnect();

# Notes:
#       NUM_OF_FIELDS
#               Number of fields returned by a SELECT or
#               LISTFIELDS statement.  You may use this for
#               checking whether a statement returned a result: A
#               zero value indicates a non-SELECT statement like
#               INSERT, DELETE or UPDATE.
