As someone pointed out in the comments, the docker-compose file you provided is very relevant to your question.
The documentation for links in docker-compose files says
Containers for the linked service will be reachable at a hostname identical to the alias, or the service name if no alias was specified.
In your case, the database container is named db, so resolving db host from the PHP container should point you at the MySQL container. Replacing localhost with db in your config file should allow the PHP container to connect to MySQL.