Correct and secure way to access a custom SQL database in a custom PHP template file

Correct and secure way to access a custom SQL database in a custom PHP template file