I know that it is not clearly expressed in documentation but could you change your code to be:
if(!empty($row)) {

and could you also fix any indentation below to reflect the same change when you want to have { on the same line