$query = qq(SELECT * FROM users WHERE userid = \'$username\'); $sth = $dbh->prepare($query); $sth->execute; $userCount = 0; while (($userid, $pwd) = $sth->fetchrow_array) { print $userid . "is a valid user"; $userCount = 1; } if($userCount == 0){ print "invalid user"; }