for $i (0 ..$#tables_to_query) { $sql = "select count(*) from ".$tables_to_query[$i].";"; $sth = $dbh->prepare($sql) or die("Could not prepare!" . $dbh->errstr); $sth->execute() or die("Could not execute!" . $dbh->errstr); $total = $sth->fetchrow_array(); print "Total $i is $total\n"; }