For this section, you're probably better off setting all this in the while loop - otherwise you'll get errors if for some reason the query doesn't return anything.