You are not logged in.

#1 2010-09-16 00:25:31

Shaika-Dzari
Member
From: Québec, Canada
Registered: 2006-04-14
Posts: 436
Website

PHP database connection strategy

Hello everyone,

I'm currently writing a website with PHP and MySQL.

I would like to know which strategy you choose to manage database connection.

I did not find any way to have a database connection pooling in PHP like I usally use in J2EE.
My current strategy is some kind of singleton my front controller will share with all "sub controller".
So, to render a page, I'll have a single static connection ( using PDO ). Opened at the beginning and closed at the end of the page.

So, what do you use in your PHP webapps?

Regards.

Offline

#2 2010-09-17 15:59:12

Shaika-Dzari
Member
From: Québec, Canada
Registered: 2006-04-14
Posts: 436
Website

Re: PHP database connection strategy

Up!

No one are creating their our database connection business?

Are you using zend, pear or something?

Regards.

Offline

#3 2010-09-17 16:11:41

Nagian
Member
From: Altach, Austria
Registered: 2009-08-03
Posts: 70

Re: PHP database connection strategy

I am using the Zend Framework, but have you already read the Zend Framework Quickstart? There are a few often needed components explained, also the database connection.

http://framework.zend.com/manual/en/lea … start.html

Offline

#4 2010-09-17 16:12:56

litemotiv
Forum Fellow
Registered: 2008-08-01
Posts: 5,026

Re: PHP database connection strategy

Shaika-Dzari wrote:

My current strategy is some kind of singleton my front controller will share with all "sub controller".

This. smile

Open just on the start of the page though, collect all data in an object, close the connection and then print out.


ᶘ ᵒᴥᵒᶅ

Offline

#5 2010-09-17 16:26:58

zenlord
Member
From: Belgium
Registered: 2006-05-24
Posts: 1,223
Website

Re: PHP database connection strategy

litemotiv wrote:

Open just on the start of the page though, collect all data in an object, close the connection and then print out.

Care to give me an example, or point me to an online example of this?

I'm merely an amateur, and don't understand things like 'singletons' - I do think that I currently code in MVC, but I would like to know more about design patterns. When it comes to db-connections, I think I open the connection, load all the needed data and close the connection. Don't know whether I 'collect the data in an object', though neutral

Offline

#6 2010-09-17 16:51:19

litemotiv
Forum Fellow
Registered: 2008-08-01
Posts: 5,026

Re: PHP database connection strategy

zenlord wrote:

Care to give me an example, or point me to an online example of this?

If you have a class that makes a database connection, making it a singleton is a way to construct it only once and then reuse it throughout your project without having to create new instances. So the singleton basically is just a class (with a bit of extra code), and an instance of a class is called an object.

Here is an example of how to create a singleton in PHP: http://en.wikipedia.org/wiki/Singleton_pattern#PHP


ᶘ ᵒᴥᵒᶅ

Offline

#7 2010-09-17 17:07:44

Shaika-Dzari
Member
From: Québec, Canada
Registered: 2006-04-14
Posts: 436
Website

Re: PHP database connection strategy

zenlord wrote:
litemotiv wrote:

Open just on the start of the page though, collect all data in an object, close the connection and then print out.

Care to give me an example, or point me to an online example of this?

I'm merely an amateur, and don't understand things like 'singletons' - I do think that I currently code in MVC, but I would like to know more about design patterns. When it comes to db-connections, I think I open the connection, load all the needed data and close the connection. Don't know whether I 'collect the data in an object', though neutral

If you search a very good book, my suggestion is:
http://www.amazon.com/Objects-Patterns- … 982&sr=8-1

Seriouly, a must have! smile


litemotiv wrote:

Open just on the start of the page though, collect all data in an object, close the connection and then print out.

Great to see I'm not completely wrong big_smile


Nagian wrote:

but have you already read the Zend Framework Quickstart?

Yep, I know Zend. But I want to create something without any framework to learn and improve my php knowledge.

Offline

Board footer

Powered by FluxBB