php-page with db connection

This file to access WordPress default functions has to be inside the WordPress folder. It’s the best practice to create a plugin if you want to add custom features. WordPress provide directory path and URL’s functions with which you can get the path of any specific file. Please visit the following links to understand how these functions work.
https://developer.wordpress.org/reference/functions/plugin_dir_url/
https://developer.wordpress.org/reference/functions/plugin_dir_path/

Absolute path to the WordPress directory is defined by a constant named as ABSPATH.

And for database you can use WordPress global database object global $wpdb;