Answer the question
In order to leave comments, you need to log in
Is it possible to store a SQL query in a PHP session variable?
There is a PHP script that generates an SQL query code to the Database based on the filters selected by the user and receives data from the database with this query. Filters are set by the user using JavaScript and passed as variables via AJAX to the PHP script. The fact is that when the user scrolls the page to the very bottom through AJAX, more content is requested and the PHP script re-generates the same SQL request code (the user did not change the filters). I thought it was bad to generate the same SQL every time a user loads content, for optimization I will generate new SQL only when the user changes filters, and save the current SQL code to the session in order to load new content with it.
Question: Is it possible to store SQL code in a PHP session variable? Will it be safe and will the user not be able to get the SQL code?
Answer the question
In order to leave comments, you need to log in
It is quite safe to store a SQL query in a session.
This is usually not done, because query generation takes negligible time compared to data fetching. For optimization, it is better to optimize queries, and if possible, use caching of query results or content generated based on these results. The query cache is configured in the database server settings. The cache on the backend side can be both a cache of the results of queries to the database, and a cache of the content that will be generated based on these results, Redis and Varnish are perfect for this.
The cache of the request itself makes sense to use if additional requests are used to form the final request and these requests cannot be cached on the database side.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question