In the first one, PHP executes the query each time the loop enters new iteration, so the cursor (pointer to the row that goes forward each time you iterate over the result) does not move forward in the array of results. You get the whole bunch of same results each time and each time start from the beginning since the query is just executed.
In the second one your query is executed only once, and then PHP is iterating over the complete array of results while the cursor is moving forward and not being reset to zero on each iteration. That one is probably what you need.