I have this Perl script and my server admin said it is using WAY too much resources. Any suggestions on how to improve the script so it will run faster and better (uses MySQL database)?
What this script does is count the number of impressions of a banner. So when it is called like this:
<img src="path/banner.cgi?BANNER_ID">
the script adds +1 to the number of impressions, selects URL ("src" of the banner from the database and redirects to the image.
### VARIABLES ###
$database_name="database_name";
$database_user="database_username";
$database_pass="password";
### END VARIABLES ###
use DBI;
# Get banner ID
$id = $ENV{'QUERY_STRING'};
$dbh = DBI->connect( "dbi:mysql:$database_name", "$database_user", "$database_pass", {RaiseError=>1,AutoCommit=>1} ) or die("Can't connect to the MySQL database. Please check your database host, username and password"
# add +1 to impressions
$sth = $dbh->do("update banners set impressions=impressions+1 where id=\"$id\"" or db_err("Unable to execute query", $dbh->errstr);
# Get the src parameter of the banner
$sth = $dbh->prepare("select `src` from `banners` where id=\"$id\""
$sth->execute or db_err("Unable to execute query", $dbh->errstr);
($src) = $sth->fetchrow_array;
# Print the image
print "Location: $src\n\n";
Any ideas? Thanks!
What this script does is count the number of impressions of a banner. So when it is called like this:
<img src="path/banner.cgi?BANNER_ID">
the script adds +1 to the number of impressions, selects URL ("src" of the banner from the database and redirects to the image.
### VARIABLES ###
$database_name="database_name";
$database_user="database_username";
$database_pass="password";
### END VARIABLES ###
use DBI;
# Get banner ID
$id = $ENV{'QUERY_STRING'};
$dbh = DBI->connect( "dbi:mysql:$database_name", "$database_user", "$database_pass", {RaiseError=>1,AutoCommit=>1} ) or die("Can't connect to the MySQL database. Please check your database host, username and password"
# add +1 to impressions
$sth = $dbh->do("update banners set impressions=impressions+1 where id=\"$id\"" or db_err("Unable to execute query", $dbh->errstr);
# Get the src parameter of the banner
$sth = $dbh->prepare("select `src` from `banners` where id=\"$id\""
$sth->execute or db_err("Unable to execute query", $dbh->errstr);
($src) = $sth->fetchrow_array;
# Print the image
print "Location: $src\n\n";
Any ideas? Thanks!