Planetarion Forums

Planetarion Forums (https://pirate.planetarion.com/index.php)
-   Programming and Discussion (https://pirate.planetarion.com/forumdisplay.php?f=57)
-   -   PHP and DB connection. (https://pirate.planetarion.com/showthread.php?t=197209)

MrLobster 30 Nov 2008 03:45

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?

Phil^ 30 Nov 2008 17:44

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.

MrLobster 1 Dec 2008 03:28

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.

Remy 5 Dec 2008 10:19

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

Mzyxptlk 5 Dec 2008 11:04

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.

qebab 7 Dec 2008 10:34

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.


All times are GMT +1. The time now is 21:27.

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