Print out error if query statment used in a loop