my ($sth, $sum) = ($dbh->prepare ("select sum (whatever) from ...")); $sth->bind_columns (\$sum); while ( long running loop ) { if ($sth->execute and $sth->fetch) { # My $sum is now valid } else { print STDERR "Something went wrong: ", $sth->errstr, "\n"; } }