You should add a check for results before trying to foreach them. Otherwise you will get an error when no results are found.