I haven't tested it, but I think your script hangs because arguments in PHP are passed by value.
Thus, when you call getResult($result)
gets a copy of $result
operates on this copy. Any changes it does to the internal structure of $result
are not propagated back to the original. So, with the next iteration pg_fetch_array
returns the same row again because it gets another copy of the unchanged original.
Try to change getResult
and see if it helps.