$query object definition outside of conditional block [corrected]

Only the definition of $query is needed outside the conditional, the
chained methods belong within.