#522 change query result from foreach to while