SQL queries to another wordpress site

Putting an important credentials in functions.php is not a good idea. The better way is you can utilize WP REST API from your second site to handle GET and POST method for the data and using global $wpdb variable inside the code.

You can either create a custom endpoint for that by making a custom plugin or insert the code into functions.php. You may need to configure the permission_callback parameter for the API endpoint whether it is being used internally or publicly.

Read more on REST API Handbook

tech