User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion

Reply
Thread Tools Display Modes
Unread 30 Nov 2008, 03:45   #1
MrLobster
Commander in Briefs!
 
MrLobster's Avatar
 
Join Date: Dec 2000
Location: UK
Posts: 783
MrLobster has much to be proud ofMrLobster has much to be proud ofMrLobster has much to be proud ofMrLobster has much to be proud ofMrLobster has much to be proud ofMrLobster has much to be proud ofMrLobster has much to be proud ofMrLobster has much to be proud ofMrLobster has much to be proud of
PHP and DB connection.

So I sit here at a point when I want to do a big project in WAMP (Windows / Apache / MySQL / PHP).

I've done php for a few years now, but nothing major apart from my own website.

Whenever I have queried a DB, I always open a new connection first. This is because I include a seperate file which does a certain feature into index.php (e.g. newsfeed.php).

Now I'm thinking is it efficient to open up a new DB connection everytime i load a "newsfeed.php" or some other thing, or should I just open a connection at the start of index.php and close it at the end, and never open a connection anywhere else.

As it stands i might be opening 5-10 different connections (and closing them) during the loading of index.php.

Or is opening a connection actually not that great an overhead?
__________________
<Kila> WHAT HAVE YOU DONE WITH MY PRECIOUS FORUMS
<Zeyi> 24h forum closure
<Zeyi> all posts recalled

"he's got a proven track record when it comes to showy art composition" - Tommy

<Sigi> Light: can I ask u how many open internet-windows u always have?
<MrLobster|PM> i have 2, the pa page, and the website for naked light pictures
<Ave> both has bad gfx
MrLobster is offline   Reply With Quote
Unread 30 Nov 2008, 17:44   #2
Phil^
Insomniac
 
Phil^'s Avatar
 
Join Date: May 2003
Posts: 3,583
Phil^ spreads love and joy to the forum in the same way Jesus wouldPhil^ spreads love and joy to the forum in the same way Jesus wouldPhil^ spreads love and joy to the forum in the same way Jesus wouldPhil^ spreads love and joy to the forum in the same way Jesus wouldPhil^ spreads love and joy to the forum in the same way Jesus wouldPhil^ spreads love and joy to the forum in the same way Jesus wouldPhil^ spreads love and joy to the forum in the same way Jesus wouldPhil^ spreads love and joy to the forum in the same way Jesus wouldPhil^ spreads love and joy to the forum in the same way Jesus wouldPhil^ spreads love and joy to the forum in the same way Jesus wouldPhil^ spreads love and joy to the forum in the same way Jesus would
Re: PHP and DB connection.

i dont think its a massive overhead really - but its still an overhead. (theres also the whole max-connections thing in mysql to consider )
The problem is - if you keep and re-use a single connection then other queries have to wait for previous ones to complete. On the other hand, php is interpreted. its not like a multithreaded program where things can proceed without others having finished so the only way its going to be an issue is when you have *lots* of people viewing at once

If you want to design your way around the problem, you could look into database connection pooling , object orientated design and the singleton design pattern.
Those should give you a nudge in the direction of a possible own-build solution to it, but for a site without major traffic, its going to be overkill.
__________________
Phil^

Last edited by Phil^; 30 Nov 2008 at 17:49.
Phil^ is offline   Reply With Quote
Unread 1 Dec 2008, 03:28   #3
MrLobster
Commander in Briefs!
 
MrLobster's Avatar
 
Join Date: Dec 2000
Location: UK
Posts: 783
MrLobster has much to be proud ofMrLobster has much to be proud ofMrLobster has much to be proud ofMrLobster has much to be proud ofMrLobster has much to be proud ofMrLobster has much to be proud ofMrLobster has much to be proud ofMrLobster has much to be proud ofMrLobster has much to be proud of
Re: PHP and DB connection.

Yeah it only ever opens 1 connection at any one time, so no problems with too many connections.

But i will certainly look into your suggestions.
__________________
<Kila> WHAT HAVE YOU DONE WITH MY PRECIOUS FORUMS
<Zeyi> 24h forum closure
<Zeyi> all posts recalled

"he's got a proven track record when it comes to showy art composition" - Tommy

<Sigi> Light: can I ask u how many open internet-windows u always have?
<MrLobster|PM> i have 2, the pa page, and the website for naked light pictures
<Ave> both has bad gfx
MrLobster is offline   Reply With Quote
Unread 5 Dec 2008, 10:19   #4
Remy
Ex-Head Multihunter
 
Remy's Avatar
 
Join Date: Sep 2003
Location: At home
Posts: 900
Remy has much to be proud ofRemy has much to be proud ofRemy has much to be proud ofRemy has much to be proud ofRemy has much to be proud ofRemy has much to be proud ofRemy has much to be proud ofRemy has much to be proud of
Re: PHP and DB connection.

I open a connection once at the start of a PHP file, and close it at the end.
Whenever i include other php files in the first php, i do not reopen, UNLESS the included file is sometimes used on its own too.

Works great and no fuss
__________________
R02.0-R4.0: [noob]
R05.0: [Wrath]/[Fury]
R06.0: Quit after 1 week
R7-9: Had an account, but didnt play seriously
R09.5: []LCH[] Officer
R10.0: []LCH[] HC (Rank #9, #1 Gal)
R10.5-R18.0: []LCH[] HC Scanner!
R18.0-R33 : Multihunter, Head MH
R34-.. : [CT] HC
Remy is offline   Reply With Quote
Unread 5 Dec 2008, 11:04   #5
Mzyxptlk
mz.
Alien Invasion Champion, Submarine Champion, Tiger Punch Champion, Barts Watersports Adventure Champion
 
Join Date: Aug 2005
Posts: 8,587
Mzyxptlk has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Mzyxptlk has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Mzyxptlk has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Mzyxptlk has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Mzyxptlk has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Mzyxptlk has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Mzyxptlk has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Mzyxptlk has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Mzyxptlk has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Mzyxptlk has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Mzyxptlk has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
Re: PHP and DB connection.

When programming communication with a database, I try to make a habit of making it all go through a single class. That way you never open more connections than needed.
__________________
The outraged poets threw sticks and rocks over the side of the bridge. They were all missing Mary and he felt a contented smug feeling wash over him. He would have given them a coy little wave if the roof hadn't collapsed just then. Mary then found himself in the middle of an understandably shocked family's kitchen table. So he gave them the coy little wave and realized it probably would have been more effective if he hadn't been lying on their turkey.
Mzyxptlk is offline   Reply With Quote
Unread 7 Dec 2008, 10:34   #6
qebab
The Original Carebear
 
Join Date: Nov 2004
Location: Trondheim, Norway
Posts: 1,048
qebab is an inspiration to us all and we should try to be more like himqebab is an inspiration to us all and we should try to be more like himqebab is an inspiration to us all and we should try to be more like himqebab is an inspiration to us all and we should try to be more like himqebab is an inspiration to us all and we should try to be more like himqebab is an inspiration to us all and we should try to be more like himqebab is an inspiration to us all and we should try to be more like himqebab is an inspiration to us all and we should try to be more like himqebab is an inspiration to us all and we should try to be more like himqebab is an inspiration to us all and we should try to be more like himqebab is an inspiration to us all and we should try to be more like him
Re: PHP and DB connection.

You probably want to look into a library that abstracts away most of the SQL for you. If PHP has good frameworks for this, it'll also include things like connection pooling, and it'll probably be clever enough to close your connection automatically when you're done with it.

Edit: Another bonus being that it'll be easier to use another implementation of SQL in the future if you so choose. But I guess that's less useful in PHP than other languages, given the integration PHP actually has with MySQL.
__________________
If at first you don't succeed, try, try again. Then quit. No use being a damn fool about it.

Oh crap, I might be back. I should take my own advice.
qebab is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Forum Jump


All times are GMT +1. The time now is 10:36.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018