How to store database connection?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jlising
Forum Commoner
Posts: 33
Joined: Mon Mar 19, 2007 1:48 am
Location: Pampanga, Philippines
Contact:

How to store database connection?

Post by jlising »

Hello,

How can I store my database connection so that I can avoid connect and disconnect in every page?

Many Thanks!
Jess
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: How to store database connection?

Post by jayshields »

You can't really do that, although you can do things to simulate what you're suggesting.

The easiest solution is to put your database connection code into a PHP file of it's own and require() it on each page. A more complex solution would be to use a front controller which handles rudimentary tasks such as database connections, sessions, templating, etc., this idea is better for larger projects.
User avatar
jlising
Forum Commoner
Posts: 33
Joined: Mon Mar 19, 2007 1:48 am
Location: Pampanga, Philippines
Contact:

Re: How to store database connection?

Post by jlising »

Hi

Someone told me to use serialize() and unserialize() functions to store database connections. I know the syntax of the two but can anyone show me how to use them?

Many Thanks!
Jess
Post Reply