fixed some overlooked TABLE_PREFIX  and check numrows before running foreach