Forum Moderators: phranque

Message Too Old, No Replies

Database Driven Websites Tip

Database, web development, asp, php, mysql

         

mrfori

3:40 am on Feb 25, 2004 (gmt 0)

10+ Year Member



Hi Guys,

Do you recommend connecting and disconencting from DB everytime?
Or storing the $con link in Session and reuse in every page.

thanks

txbakers

3:54 am on Feb 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I like to connect each time, then close the connection each time.

I also destroy the connection object.

Maybe it's better with one connection, but I don't like leaving it open very long.

grandpa

12:10 pm on Feb 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I also destroy the connection object.

I knew was forgetting something. On a page with multiple opens would this be ok once, at the end, or better after each process?

txbakers

12:51 pm on Feb 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



at the bottom of my pages I have a section:

rsChoirs.Close();
rsChoirs=null;

for each connection object.

webadept

1:22 pm on Feb 25, 2004 (gmt 0)

10+ Year Member



It's not a good idea to use these, unless you have an overwelming need to have them.

[php.net...]

grandpa

1:01 pm on Feb 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If I'm using mysql_connect there isn't a necessity to close manually each time is there? I understood the documents to say the close was automatic.

I'm asking from a good practices POV.

txbakers

1:09 pm on Feb 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It probably is automatic, but I want it closed as soon as possible - lingering connections will eat up your bandwidth and slow the process down.

grandpa

1:36 pm on Feb 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That's interesting.. I closed each and had a sense that the page was actually running slower. I'm making 9 requests from my page. It would almost make more sense to open the db once and echo everything from the first request to the last, then close once.

I know its off topic but I've one more thought. If I fail to get a connection the page obvioulsy won't load past the first bit of script. Is there an easy way to detect this and automatically load an html version of that page?

mattur

1:49 pm on Feb 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



grandpa: putting up and tearing down db connections is an "expensive" process, so opening a connection once and re-using it for multiple queries will (all other things being equal) speed up page processing time.

txbakers

3:32 pm on Feb 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



same thing with creating connection objects. Just create one and use it over and over.

grandpa

9:01 pm on Feb 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



PHP is very new to my "list of things to do", but of everything new I've run across it seems like the most natural. I assumed it would be better to minimize the db connections but still, each was written seperately. Now that I know the page works I can optimize the code. Natural or not, I still have a few things to learn about php.

Like the difference between a connection object and a db connection...

What about the situation of not being able to connect? It must happen from time to time, for any number of reasons.

mrfori

10:14 pm on Feb 26, 2004 (gmt 0)

10+ Year Member



My short experience tells me. That if I don't optimized or comment my code as I go .. I never do it.
Because you may end up re-writing code and most times you can't afford to do that.
ASP has an interesting thing (apologies to microsoft enemies) you can query and bring the whole table to your result set .. and then you can filter your result set at run-time without having to go to DB. If you are doing queries to the same table I believe its faster.

MySQL should have allow_persistent turned ON before you can do mysql_pconnect().

If MySQL doesnt have max_links Unlimited you do have to close your connection all the time.

From all posts .. I get that "best practice" is to open and close connections on every pagec and we really need to justify the use of mysql_pconnect. Another good practice in terms of portability I believe is the use of Pear DB (DB.php) but it does compromise speed a bit.