use Switch; use DBI; sub rec_exists; my (@retRows, @row, @tmp, $retRecs); while ($line = <>) { chomp($line); if ($line eq "q") { goto Exit; } else { $retRecs = rec_exists($line,\@retRows); } $tmp = @retRows[0]; @row = @$tmp; ($nKey, $fN, $lN, $bN, $hN, $mN) = @row; print "$nKey,$fN, $lN, $bN, $hN, $mN\n"; } Exit: exit(); sub rec_exists { my ($searchType,$searchParm,$retArrayRef) = split(/,/,@_[0]); my $dbh = DBI->connect("DBI:mysql:database=aaaaas;host=localhost","bbbbbb","cccccccccc",{'RaiseError' => 1}); my ($found, @row, @rows, $nKey, $sth); $rowRef; @rows = (); switch($searchType) { case "1" { # search using the nKey $nKey = $searchParm; chomp($nKey); $nKey =~ s/^\s+//; $nKey =~ s/\s+$//; $sth = $dbh->prepare("Select * from sContacts where nKey like '$nKey%'"); $sth->execute(); $found = 0; while ( $rowRef = $sth->fetchrow_arrayref ){ $retArrayRef->[$found] = [ @$rowRef ]; $found++; } } } $sth->finish; $dbh->disconnect; return $found; }