#!/usr/local/bin/perl

# Perl/MySql Based Random Ad Banner Rotator
# Version 3.0
# adban468.pl
# By Mike Calabrese
# http://www.mikecalabrese.com/index.shtml
# 01-17-2005

# This program is available for free on an AS-IS basis.  I am not
# responsible for consequential damages, loss of income or whatever
# else that may happen as a result  of the use of this program.

# Query the database and get the max number of ad banners or
# reference number

use DBI;

# Replace databasename with the name of your database below
my ($dsn) = "DBI:mysql:databasename:localhost";

# Replace "username" with your database login name below
my ($user_name) = "username";

# Replace "userpwd" with your database password below
my ($password) = "userpwd";
my ($dbh, $sth);
my (@ary);
$dbh = DBI->connect ($dsn, $user_name, $password, {RaiseError => 1});

# Issue the select statement that gets the member information
$sth = $dbh->prepare ("select count(*) from adban468");
$sth->execute ();

while (@ary = $sth->fetchrow_array ()) {
$reference_number = "@ary";
}


# Disconnect from the database
$sth->finish ();
$dbh->disconnect ();

# Generate a random number between 1 and the reference number.
$rand_ref_num = int(rand($reference_number)) + 1;

# Make the database call utilizing $rand_ref_num.
use DBI;

# Replace databasename with the name of your database below
my ($dsn) = "DBI:mysql:databasename:localhost";

# Replace "username" with your database login name below
my ($user_name) = "username";

# Replace "userpwd" with your database password below
my ($password) = "userpwd";
my ($dbh, $sth);
my (@ary);
$dbh = DBI->connect ($dsn, $user_name, $password, {RaiseError => 1});

# Issue the select statement that gets the member information
$sth = $dbh->prepare ("select ad_banner "
   . "from adban468 where reference_number = '$rand_ref_num'");
$sth->execute ();

$counter1 = 1;
while (@ary = $sth->fetchrow_array ()) {
$ad_banner = "@ary";

# Give out the results
print "Content-type: text/html\n\n";
print "$ad_banner";
$counter1 = $counter1 + 1;
}

# Disconnect from the database
$sth->finish ();
$dbh->disconnect ();