April 14, 2012

When to load database in php

Question by EvilDesire

This is a general question.

I have a website with a page showing many elements of my database.
And in this page I have a script where I’m loading these elements.

The the thing I want to know is it alright to load the database at the display of the page, knowing that everytime user will click and reload the page, my script will reload everything again from my database?

Is it a proper way to do it? Like creating an init.php at the first loading of the website, and storing my elements into a $_SESSION?

I don’t know if I managed to explain my problem, so please ask me more informations if needed.

Answer by Starx

The concept is pretty simple.

If you are sending a new request, it is wise to always start a new connection when needed and close it when its done.

But do not store the connection or any results in SESSION, as this can cause security issues. Creating a common script to start adn close a connection is the way to go.

Having said that look at persistent connections is it probably the thing you want.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+


Please fill the form - I will response as fast as I can!