At 11:07 AM +1200 4/2/06, Jasper Bryant-Greene wrote:
tedd wrote:
> > > I always close the connection right after my
> > query -- force of habit. It's like leaving the
> > toilet seat up, it's only going to get you into
> > trouble.
>
> So you close it after every query and then re-open it later for the
> next query? I don't see that as a good idea.
> >
> No, you leave it open until you're done with the database.
Reading Ted's post didn't give this impression. I wanted to make sure
he wasn't doing it that way.
Chris et al:
Actually I am. When I need something from the dB, I open it, get
the information and close it. It's like opening a drawer, getting
what you need, and then closing the drawer. Where's the problem?
Uh, what if you want to do more than one query in a single request?
You aren't seriously suggesting you would connect and disconnect
from the same database multiple times within the same request?
No, I'm not. What I am suggesting is that if you are done with your
query, and are not immediately asking another, then hang up. If
another segment of your code wants to access the dB again, then open
it as you want. The practice makes for more modular code and keeps
the right connections active when they are needed.
In my experience, connecting to the database takes up more than half
of the execution time of the average database-driven PHP script (I
said *average*, there are exceptions). You don't want to be doing it
multiple times if you don't have to.
In my experience, it's better, in many ways, to put things away when
you're done using them. This tread started out with "What's the
purpose of closing a connection? After all, when the script finishes,
doesn't the dB automatically close?" While that's true, I personally
think it's a bad habit that could lead to problems. Your mileage may
vary.
tedd
--
--------------------------------------------------------------------------------
http://sperling.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php