move $dbhandler and $selected outside the comparison