#Assuming $sth has just been executed with a "show tables" query while(@tmp = $sth->fetchrow_array()) { my $tablename = $tmp[0]; my $sth2 = $dbh->prepare("select count(*) from $tablename"); $sth2->execute(); $numrows = ($sth2->fetchrow_array())[0]; if($numrows > 0) { push @tables, $tablename; } }