Freeing mysql query and closing connection