Accidentally closed mysql connection in for loop