Create a "constants" file where you define your database connection variables and store it outside your root directory (security)
Include this file in the scripts you want to make a db connection.
This might explain a good way.

The second sounds like a normal practice in all language, so yeah, thats the way to do it.