Rewrites/fixes for while() loops with merge_array() and SQL_FETCHARRAY() usage