depends on what you want.
faster should be the if-way (if it really works

)
but nicer is the reapeated while.
if it wouldn't produce too much queries on an often called page, i'd use the while-method.
if it's an page where it's important of how much queries are on, i'd use the if method, but code it better readable in a half while or recursive method to get the querystring