..... ## Rather then getting '$all_info' with one fetch, then getting a N_NUM with a seperate fetch, do it all at once. ## (assuming you are using two tables). Now you can skip the sub all together. my $tablerows = ""; ## Build the table row data, then add it to print.... my $sth = $dhb->prepare("SELECT a.product, b.number FROM tableA a, tableB b WHERE a.id = b.id"); ## or whatever $sth->execute(); $sth->bind_columns(\my($product,$number)); ## use bind_columns while ($sth->fetch()) { my $found = ($number) ? 'FOUND' : 'NOT FOUND'; $tablerows .= qq{ $product$found\n}; } ## Using here-is print < $tablerows HTML ....