English 中文(简体)
每当该页装上时,这些神秘的问询是否有发生?
原标题:Does each of these mysql queries run each time the page is loaded?
  • 时间:2011-10-04 12:26:03
  •  标签:
  • php
  • mysql

在文稿每页一页的汇辑中,我有以下代码,以查找优先用户的语言档案:

if ($_SESSION[ id ]) { // if logged in

    $language = mysql_result(mysql_query("SELECT default_lang FROM employees WHERE id =  $_SESSION[id] "), 0);

    include "languages/".$language.".php"; 

} else { // If not logged in, use system default language

    $language = mysql_result(mysql_query("SELECT config_value FROM system_config WHERE config_name =  language "), 0);

    include "languages/".$language.".php"; 
} 

我的问题是,在访问者重新翻一页时,这种问问问问问是否每过一次? 看到某一页的我的片面问询总数最容易些,我常常在论坛中看到这一点,例如:20次在二十次秘密中回答。

最佳回答

In general, it is a good idea to reduce the amount of queries, but I would rather keep the code clean early on. Caching will introduce all kinds of problems and unless you really need it because the query is slow, it s a waste of time that will increase the code complexity.

Simple queries that affect a single table and return a single value are quite fast. Chances are, your attempts to cache the value will not give a significant benefit.

为了了解业绩,你应首先使用简介员。 它将向你们展示贵方言中所需要的时间,并可能赢得你们的期望。

就发展环境而言,我与KCacheGrind一样。 http://devzone.zend.com/article/2899"rel=“nofollow”

问题回答

是的,一个或另一个时间是every ——这是你为获得会议而已经做过一些每个用户I/O的事情——你可以这样做。

$language=$_SESSION[ lang ];
if (!$language) {
    if ($_SESSION[ id ]) {
       $language = mysql_result(mysql_query("SELECT default_lang FROM employees WHERE id =  $_SESSION[id] "), 0);
    } else {
       $language = mysql_result(mysql_query("SELECT config_value FROM system_config WHERE config_name =  language "), 0);
    }
    $_SESSION[ lang ]=$language;
}
include "languages/".$language.".php";

甚至只是请求中使用接受语文(假设你使用标准命名,并使用所有语文)。

list($language)=explode( ; , $_SERVER[ Accept-Language ]);
include "languages/".$language.".php";

(使用申请负责人的上述代码需要清理一条轨道,以避免当地档案列入vul)

两个查询中,有一个将每页处理一次。

如果你不希望,你也可以在你的届会中将缺损——兰变数。

Yes, it will be executed every time page is reloaded. However, technically, the second time it is faster since it picks up from cache or already has the query plan. Please anyone confirm if I am wrong?





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签