What fetchrow_array returns in scalar context is undocumented. $data1 = $sth->fetchrow_array(); is wrong. If you want to the first field, use ($data1) = $sth->fetchrow_array(); #### Is the contents of $dberror text of HTML? You put both into it. At worst, this could be an opening for a cross-site scripting attack. #### All over, you are instructing Perl to ignore prototypes. Why do you use & on your subroutine calls? #### I note a distinct lack of my. Use use strict;! #### Something tells me you don't use warnings either. ("if ($data1 ne $digest1)" would warn if fetchrow_array returned zero rows.) Use use warnings;! #### Why is six in quotes in "if (scalar @row1 == "6")"? You want to compare with the number 6, not the string "6" (which would be converted to a number by ==). The scalar is useless too. == creates a scalar context, so scalar is redundant. #### if (@row1 == 6) { other code here ... }; #### for my $row ( 1 .. $#rows ) { if (@{ $rows[$row] } == "6") { my @entry = (@required , @row); ... } }