Some recent discussion in the chatterbox showed a few people hadn't seen bind_columns, so I grabbed some code I recently wrote using that, abstracted it a bit, and included it here as a cut-n-paste model of how to use that. Enjoy!
use DBI;
my $dbh = DBI->connect("dbi:mysql:database", "user", "password",
{ RaiseError => 1 });
$dbh->do("SET OPTION SQL_BIG_TABLES = 1"); # for mysql
my $sth = $dbh->prepare(<<'_END');
SELECT
field1, field2, sum(field3) as Total
FROM some_database
WHERE field2 LIKE 'random%' AND field1 BETWEEN ? and ?
GROUP BY field1, field2
ORDER BY Total DESC
_END
$sth->execute('a', 'k');
$sth->bind_columns(\my($field1, $field2, $sum));
while ($sth->fetch) {
printf "%20s %20s %s\n", $field1, $field2, $sum;
}
$sth->finish;
$dbh->disconnect;