Today multiple questions came to me about how web servers work with a basic code of request in database and display to the front.
For the example of a blog where we want to display our posts I don't know any other way than
- Query the database then stock the result in a php variable
- Foreach the variable in the front to display the different contents
as simple as that but the question is :
Does the database query each time a visitor browse the web page supposed to display our posts ?
For me it has always worked as this. But now I'm asking myself why do we make requests on a table that we (the writers of the posts) update like one time each week everytime we visit the page ? In the case of a blog with thousands daily visitors while we update it every week, Do we query the database that much time ?
The question is the same for the php process that "foreach" every posts which require a little bit more process for the server before displaying the web page. Multiplied by the number of visitor.
If it really works as I think, how do we avoid all of these ?
Its sounds like you want to use Caching - there are simple ways to do this, and libraries/products that can help you see this article and this library