I have pages that have content dynamically added through mySQL queries. Most of these pages make use of php file includes, so my header, navigation, body, and footer are in separate files. My MySQL connection code is also in a separate file. Currently I am connecting to my database, querying content, echoing the conent, and closing the database in EACH file.
My question is this: Is there any reason for or against having my connection script included and database opened and closed in each file (header, navigation, body, and footer) since they are all part of the same page? Or should ALL of my PHP code connecting to the database be in the same file?
I am self taught, and this sort of theory has never really been explained in any of the sources I have learned from.
This is what you should aim for. Opening and closing db connections in each file violates the DRY principle.