Something like this could work:
my $sql = 'select rec_data from foo where id in (';
$sql .= join ',', ("?") x @queryIDs;
$sql .= ') order by id';
my $sth = $dbh->prepare($sql);
$sth-> execute(@queryIDs);
# rest of code will be the same as you have now