my $handle = $syb->prepare($query); while(my @row = $select->fetchrow_array()) { # assuming that $row[0] is what you want to use in the # next query if($row[0] == 'ok') # it passes validation, yay { $handle->execute($row[0]); # fetch all the rows here if you want } else { # this is how you trap errors, leave this blank if you wish } }