Jump to content


This topic is now archived and is closed to further replies.


A few questions ..

Recommended Posts

I have a community website and I do lots of different queries at different times in the webpage.


1. Is it better to open and close a MySQL connection everytime I need to gather info on the db with mysql_connect and mysql_close or to open it in the beginning and close it at the end of the page? (Knowing that i can have up to 20 group queries in one page)


2. If it\'s better to open it at the beginning and close it at the end, would it be better to use mysql_connect or mysql_pconnet ??


Could someone answer these questions mentionning why it\'s the best solution. (rendering speed, security, traffic ... )

Share this post

Link to post
Share on other sites

i will ans to my best ability atm :P ..maybe someone else can add...


1. Yes i believe its better to open the connection once and close it at end of script(which php does automatically anyway) ..in the case of more than 1 or 2 queries to be executed in the same script i would say yes ..use the mysql_pconnect. Of course this is all so that the server and client wouldn\'t have to re-establish connection with each other. Yes this has to do with server load/traffic..


2. mysql_connect or mysql_pconnect? ...not sure ...i believe it depends on ur script ...lotta queries = pconnect ...little = connect ...this is what i know so far.


//anyone plz correct me if needed :)

Share this post

Link to post
Share on other sites

I dont recommentd pconnect because you have to explicitly close the connection when it its not needed.


When using the general connect its not really compulsary to do that.

Share this post

Link to post
Share on other sites

and just for mention, does




actually close persistent connections? i read somewhere that it doesn\'t, but if that\'s the case, how do you close the connection?

Share this post

Link to post
Share on other sites

So to summarize ..


holiks says it would be better for me to use pconnect since I do lot\'s of connection in the page, and there isn\'t a single page, that doesn\'t require a query.


For akitchin:

www.php.net says:

mysql_close() will not close links established by mysql_pconnect().


By reading the definition of pconnect on php.net, it looks like it works almost like a session but for MySQL but does it close if the user closes the web browser ?? Or does it stay open for other visitors ??


I also found this part on php.net that pretty much answers most of my questions:

If persistent connections don\'t have any added functionality, what are they good for?


The answer here is extremely simple -- efficiency. Persistent connections are good if the overhead to create a link to your SQL server is high. Whether or not this overhead is really high depends on many factors. Like, what kind of database it is, whether or not it sits on the same computer on which your web server sits, how loaded the machine the SQL server sits on is and so forth. The bottom line is that if that connection overhead is high, persistent connections help you considerably. They cause the child process to simply connect only once for its entire lifespan, instead of every time it processes a page that requires connecting to the SQL server. This means that for every child that opened a persistent connection will have its own open persistent connection to the server. For example, if you had 20 different child processes that ran a script that made a persistent connection to your SQL server, you\'d have 20 different connections to the SQL server, one from each child.


Note, however, that this can have some drawbacks if you are using a database with connection limits that are exceeded by persistent child connections. If your database has a limit of 16 simultaneous connections, and in the course of a busy server session, 17 child threads attempt to connect, one will not be able to. If there are bugs in your scripts which do not allow the connections to shut down (such as infinite loops), the database with only 16 connections may be rapidly swamped. Check your database documentation for information on handling abandoned or idle connections.


So if someone is browsing the webpage and idles for 3-5min, i believe that the connection will still be active, causing a +1 on the connection limit.


I will propably do mysql_connect at the beginning and mysql_close at the end for now.


If anyone can add anything on pconnect would be greatly appreciated.


Thank you


Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.