Oh my gosh, a question I can actually answer!!!!!

#! /usr/bin/perl -wT

use CGI;
use DBI;

#parse form data here or whatever else you need to do

### CONNECT TO DATABASE ###
$dbh = DBI->connect("DBI:mysql:nameofyourdatabase", "yourdatabase",
   "password") || die $DBI::errstr;

### BUILD QUERY ###
$sql = "select loc_id, $datsql, date from mrddaily where ($locsql) and 
($date) order by date";  #this is your SQL select statement

### PREPARE AND EXECUTE ###
   $sth = $dbh->prepare($sql);
   $sth->execute();

### VARIABLE TO HOLD RESULTS ###
my (@results) = ();

### RETRIEVE RESULTS AND STORE ###
   while (my @ary = $sth->fetchrow_array()) {
      push (@results, [@ary]);
   }

### COUNT RESULTING ROWS AND COLUMNS ###
   my ($rows) = scalar (@results);
   my ($cols) = ($rows == 0 ? 0 : scalar (@{$results[0]}));

print "Content-type: text/plain\n\n";

# do your HTML stuff here

### THIS IS WHERE THE RESULTS ARE OUTPUT ###
for (my $i=0; $i<$rows; $i++) {
   for (my $j=0; $j<$cols; $j++) {
      print $results[$i][$j];
   }
}

# do more HTML stuff here   

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


*note: This is not the only retrieval method.  The books I have (I use MySQL) 
are Programming the Perl DBI and a book called MySQL by Paul DuBois.  Both 
books have examples of using DBI. 

Good luck!

Michelle

Reply via email to