ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm currently developping a php - html webpage with a mysql database.
I'm wondering about the following: do you open the database link as soon as the page opens and close it when the page is closed? Or do you just open the db whenever data is needed from it and close immediately afterwards?
I'm just wondering about the latency times etc.
Depends on your requirements. If there aren't a lot of queries per page, I open and close on the spot. For projects that have a lot of queries (particularly on a single page) then I perfer per page opening/closing or persistant connections.
I don't know if you can apply it to PHP, but in enterprise J2EE you normally rely on container-managed connection pooling. Connection pooling is basically a managed set of connections that are kept open. If this is not available, then one must use their own method of connection pooling. I've heard you can use C++ or Java classes in PHP (I'm not a PHP programmer) but if this is true, you can use one of the many connection pool classes available.
Thought it's not common in "vanilla" PHP, I've seen a couple of decent connection pooling setups on some commercial projects. Can't remember specific urls of the top of my head, but they are out there...
hmm... a little bit over my head I think.
What I'm doing now is creating a database class, which contains some methods to connect, disconnect, set some parameters like the db location, user, ... and then at the beginning of my script, I make an instance of this class and have it connect to the database returning a link resource.
That single (I hope) resource is then used for all my queries and at the end of the script, I disconnect from the database and free the resource.
I don't think that this is a persistant db cnx, every time the page is requested, it runs the script and thus opens and closes the connection?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.