简体   繁体   English

将永久数据存储在php文件中以减少SQL查询的数量是一种好习惯吗?

[英]Store permanent data on a php file to reduce the number of SQL queries is a good practice?

I´m creating a new website and in the webs I´ve coded before I always have been using a php file to store permanent that is used a lot that is called with an include from the index.php. 我正在创建一个新的网站,并且在我一直使用php文件存储永久性文件之前编写的Web中,该文件经常使用index.php中的include来调用。

The purpose of that is to avoid doing extra queries as this data is used every time and doesn´t change, but now I´ve concerns if that is a good practice. 这样做的目的是避免进行额外的查询,因为每次都会使用此数据并且数据不会更改,但是现在我担心这是否是一个好习惯。

One example of that is the different languages options that the website has, that is shown everytime and I don´t want to do an extra query every time. 一个例子是网站具有不同的语言选项,该选项每次都会显示,我不想每次都进行额外的查询。

Is that a good practise or it´s better to store everything on the DB and make more queries? 这是一个好习惯还是最好将所有内容存储在数据库中并进行更多查询?

I store my language dependent strings in separate files, grouped by function, for example email. 我将语言相关的字符串存储在单独的文件中,并按功能分组,例如电子邮件。 So I would have a template called 所以我会有一个名为

lng_email

Then I add a suffix to it in the include statement. 然后,在include语句中为其添加一个后缀。 For English the example would be 对于英语,示例为

lng_email_en.php

The suffix is fetched at login and stored in the session variable. 在登录时获取后缀,并将其存储在会话变量中。

Well after 3 years since I posted the question, I end up storing the data the following way: 自发布问题3年后,我最终以以下方式存储数据:

  • Almost all the data is stored in the mysql database, where it's easy to manage. 几乎所有数据都存储在易于管理的mysql数据库中。
  • I use the $_SESSION var only for user's data that is being called and used during the user's session only. 我仅将$ _SESSION var用于仅在用户会话期间被调用和使用的用户数据。
  • I have a php file where I store general data that's being use a lot by the code and won't be changed in the short or mid term. 我有一个php文件,在其中存储代码经常使用的常规数据,短期或中期不会更改。 This way I save the code to do several queries to the database. 这样,我保存代码以对数据库进行多个查询。 I store them within a function so they can be retrieved by other function and avoiding to declare them as a global . 我将它们存储在一个函数中,以便可以被其他函数检索,并且避免将它们声明为global As an example: 举个例子:

     function retrieve_privacy_list(){ $privacy_list = array( 1=>'public', 2=>'reduced', 3=>'private'); return $privacy_list; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM