in the second "broken" part of the code. Could the problem be this simple?
Also, you don't need to create a second object for the second part, just reuse the first one. And if you do create the second one, be sure to use the close method first, and it is probably best to use an different var name that $th for the second one, especially for debugging.