k, so that does not make any sense at all.
Stepping back a step, how is this code called? Is it in a function? Is it run by some other action/hook?
Something else I just noticed, but shouldn’t have anything to do with the infinite loop, shouldn’t each row ($i) be for a different post?