Most of this is simply due to poor error handling in the script. It just happily assumes everything is good and keeps going.
Your configurations are simply invalid. Provide a valid server, username, and password to the connection and go from there. There is no way to tell if its invalid beyond that, so you need to fix it up a part at a time.
There sure seems to be a lot of repeating errors here. You're not opening and closing a connection within a loop are you?