When does $\frac{\partial f}{\partial y} =0$ imply $f(x,y)=g(x)$? Let $U\subseteq\mathbb R^2$ be an open subset and $f:U\to\mathbb R$ a continuously differentiable function such that $$\forall (x,y)\in U:\frac{\partial f}{\partial y} =0.$$
Which conditions must $U$ and $f$ fulfill for $f$ to be a function in $x$ only, i. e. $\exists g:\mathbb R\to\mathbb R\forall (x,y)\in U:f(x,y)=g(x)$?
I was thinking about $U$ having to be connected, because elsewise we could have something like $U=\left\lbrace (x,y)\in\mathbb R^2\mid y<1\vee y>2\right\rbrace$ and define $f$ as $$f(x,y)=\begin{cases}x,&y<1\\2x,&y>2\end{cases}$$ which would make the partial derivative with respect to $y$ equal to zero without $f$ being independent of $y$.
However, is $U$ being a connected space a sufficient condition? How could I prove that or what would a counterexample look like?

It's not sufficient :) if $U$ is convex, then the theorem is true. For a counterexample, think about the set $$U=[0,1]\times[2,3] \cup [1,2]\times[0,3]\cup[0,1]\times[0,1]=U_1\cup U_2 \cup U_3,$$ it is something like a horseshoe. Now define $$f(x,y)=\left\{
                \begin{array}{ll}
                  \ (x-1)^2 \ \text{if} \ (x,y)\in U_1\\
                  \ 0 \ \text{if} \ (x,y) \in U_2 \\
\ -(x-1)^2 \ \text{if} \ (x,y) \in U_3
                \end{array}
              \right.$$
Now, $U$ is connected, $\ f:U \to \mathbb{R}$ is differentiable but if you restrict $f$ to $Int(U),$ which is also connected, it follows every hypothesis but doesn't follow the thesis :). 
