Help connecting PHP to a hosted MySQL database

With my teacher hat on I’m having a bit of difficulty connecting my PHP server to an externally hosted MySQL database. Wondered if anyone in the community can help?

I can’t share the code as it has login credentials for the external host but here is a similar Repl with space for your own:

The error message that appears in the Repl with the credentials is:

Your code is good - I got it to connect to one of my remote databases - how are you presenting the $servername? Usually doesn’t have any protocols with it?

I think I’ve found the reason it didn’t work:

Access to the database via your website/presence only
Please always establish the connection to your database via your website/presence. For security reasons, it is not possible to access the database directly, for example via your local computer (external ODBC connection).

To protect your data, your database is located on a dedicated database server that is protected by a firewall.

So I’m blocked from using the database outside of webpages hosted in the same domain. Damn.