You kind of have to watch out. when you use U = mgh, your zero of potential energy is at h = 0.
when you use the other equation, the zero of potential energy is at r = infinity.
m gh is based on a constant gravity (or flat Earth assumption) good for local physics
the other equation is based on a inverse square gravity. good when far from Earth center.