use empty($row) and move above those two calls?