#!/usr/bin/perl -w # mercurus' counter script # version 0.9 # 21 April 2003 # Documentation to follow... # Before configuring this file, please run: ./configure.pl to establish the required mySQL table. # Modules. use strict; use CGI; use DBI; use CGI::Carp qw(fatalsToBrowser); # Added for debugging purposes. # Variables. my $db_database = "**********"; # The name of the mySQL database. my $db_username = "**********"; # The name of the mySQL user. my $db_password = "**********"; # The password for the mySQL user. # Main script. ## Connect to the mySQL database. my $dbh = &mysql_connect ($db_database,$db_username,$db_password); ## Print the current database value. my $current_count = &print_current ($dbh); ## Increment the database value. my $exit_code = &increment ($dbh,$current_count); ## Exit. if ($exit_code == 0) { exit; } else { die "Unknown error. Execution failed, but database updated.\n"; } # Subroutines. ## Current database value printing subroutine. sub print_current { my $dbh = (@_); ## Retrieve information from the database. my $sth_1 = $dbh->prepare ("SELECT count FROM counter;"); $sth_1->execute || die "$dbh->errstr()"; while (my $fields = $sth_1->fetchrow_arrayref()) { my $current = $fields->[0]; } ## Display that information for the user. print "Content-type: text/html\n\n"; print "$current others have gathered information here.\n"; return ($current); } ## Current database value incrementing subroutine. sub increment { my ($dbh,$current) = (@_); ## Increment the previous value. my $updated = $current + 1; ## Write the new value to the database. my $sth_2 = $dbh->prepare ("UPDATE counter SET count='$updated' WHERE count='$current';"); $sth_2->execute || die "$dbh->errstr()"; return ($exit_code); } ## MySQL database connecting subroutine. sub mysql_connect { my ($db_name,$username,$password) = (@_); ## Connect to the database with the values supplied. my $dbh = DBI->connect("DBI:mysql:$db_name","$username","$password"); return ($dbh); } # End of file.