You aren't connecting to PHP and c++. PHP is a method for generating (amongst other things) html formatted web pages. It allows you to connect to a database on the same server (or others, if they are set up badly).
There are some libraries that are supposed to let you create a c++ program and have it connect to a MySQL database on the same server (and others, but MySQL is usually free on most servers). And Java has classes that do the same (I have used these).
But they would both require opening the server up to all incoming IP addresses. And you do NOT want to do that for security reasons. And, if you are using shared hosting, your provider might not allow it.
You should use whatever the appropriate commands are that let you send a request to send an html type request to your site and get the results back.
The fact that the html request is going to a PHP generated page is information your c++/java doesn't need to know.
The PHP page in your html request will connect to the local database, perform whatever functions it is supposed to and 'return' a results.
If you use your web browser to look at the source of a displayed page, you will always see html (or xml). This is independent of whether the url is referencing a page that ends with '.html', '.php' or '.asp'. They all, ultimately, output html.
Cheers,
Ancient Lady