#!/usr/bin/perl -w use strict; use CGI; use CGI::Carp qw(fatalsToBrowser); use DBI; use vars qw($q $dbh); my $dsn = "DBI:Pg:dbname=vote"; my $dbusername = undef; my $dbpasswd = undef; $dbh = DBI->connect( $dsn, $dbusername, $dbpasswd, { RaiseError => 1, AutoCommit => 0 } ) or die "Cannot connect to db: $DBI::errstr\n"; $q = new CGI; print $q->header, $q->start_html(-title => 'tally of votes', BGCOLOR => 'white'), $q->h1('Received votes'); my $sth = $dbh->prepare( q{ SELECT VN.nr AS vote, CAN.name AS candidate FROM candidates CAN, votenr VN, vote V WHERE V.vote = VN.id AND V.candidate = CAN.id ORDER BY vote } ); $sth->execute(); my $lastvote = ''; while ( my $r = $sth->fetchrow_hashref ) { if ( $lastvote ne $r->{vote} ) { print $q->br, "\n", $r->{vote}, ": "; $lastvote = $r->{vote}; } print $r->{candidate}, " "; } print $q->br, "\n";