Since you’ve posted it here I’ll assume you’re using WordPress with App Engine and not with Compute Engine.
You could review this document[1] to see if you’ve set up your database connection properly.
What you could also do is to look at the Cloud SQL logs to see if that gives you any indication on why the connection dropped.
Since you’ve mentioned it does not always work, could be an issue with Cloud SQL dropping the connection or unable to connect to some tables since you’ve also mentioned only part of the page will load when trying to access it.
[1] https://cloud.google.com/community/tutorials/run-wordpress-on-appengine-standard