PHP Novice to Ninja DB includes - pg 183

On page 183 of the book the code makes use of an include for the database connection several times in the controller where it is needed.
Could I not just include it a the top of the page outside the if statements and have it apply to the whole controller?
Is there an issue with doing this?

I don’t have a hard copy of the book to find that page. Which chapter/section is that?

chapter 6, Structured PHP programming about 7 pages in

The include is within conditions, so the connection is only made when a call to the database is required, when one of those conditions is met.

As you can see, wherever our controller needs a database connection, we can obtain it simply by including the db.inc.php file with an include statement.

Previously, we established a database connection at the top of the controller, regardless of whether the code that followed would end up needing one or not.

Only connecting to the DB when needed should make the script more efficient and in some cases possibly more secure.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.