如何只运行一次查询,而不是每次页面加载一次?

时间:2019-01-01 21:56:11

标签: php mysql

我需要一些帮助。数据库被查询所困扰,我认为这可能是其中之一。我怎么只能运行一次,这样它就不会在每次页面加载时都保持运行?

$siteqry_rs = mysql_query($siteqry);
if (mysql_num_rows($siteqry_rs) > 0) {
while ($siters = mysql_fetch_array($siteqry_rs)) {
    $tourId = $siters["Id"];

    //****************** end old *******
    $qry = "SELECT   contentgroup.Id as 
setid,contentgroup.Directory,contentgroup.Title,  
contentgroup.extrafields_PHP,
         DATE_FORMAT(contentgroup.AppearDate,'%d-%m-%Y') AS 
add_date,DATE_FORMAT(contentgroup.AppearDate,'%m-%d-%Y') AS 
add_date_format,contentgroup.SEOname AS setseoname,
         contentgroup.PreviewXML_PHP ,contentgroup.Description,         
      SUBSTRING_INDEX(GROUP_CONCAT( DISTINCT 
    plg_contentasc.ModelName),',',3)   AS       ModelName,
                              SUBSTRING_INDEX(GROUP_CONCAT( DISTINCT 
plg_contentasc.SEOname),',',3)   AS       modelseoname,
                              SUBSTRING_INDEX(GROUP_CONCAT( DISTINCT 
plg_contentasc.Id),',',3)   AS       ModelId,        
                              contentgroup.Id,
                              IF(tbl_set_top_rate.num_views IS 
NULL,0,tbl_set_top_rate.num_views) AS setrating
      FROM     contentgroup INNER JOIN  plg_contentascasc ON 
contentgroup.Id=plg_contentascasc.ContentId
                            INNER JOIN   plg_contentasc ON 
plg_contentasc.Id=plg_contentascasc.ModelId
                            LEFT JOIN  tbl_set_top_rate ON 
contentgroup.Id=tbl_set_top_rate.set_id
                            INNER JOIN sites_contentgroup ON 
sites_contentgroup.contentgroup=contentgroup.Id
      WHERE    contentgroup.websiteid='$websiteid'  AND 
sites_contentgroup.siteid='" . $tourId . "' AND (contentgroup.AppearDate 
BETWEEN '2015-01-01' AND CURRENT_DATE)
      GROUP BY contentgroup.Id
      ORDER BY contentgroup.AppearDate DESC
             limit    0,4";
    $small = 0;
    $count = 0;
    $resRated = mysql_query($qry);
    $show_li_counter = 0;
    $shoimgcounter = 0;
    if (mysql_num_rows($resRated) > 0) {
        while ($mainArray = mysql_fetch_array($resRated)) {
            $rowRated[] = $mainArray;
        }

       }
    }
}

我只想运行一次。

1 个答案:

答案 0 :(得分:1)

使用Session来存储和验证您的数据,如下所示

if(empty($_SESSION['mydata'])){

    //your query and prepare the result into $resultArray

    $_SESSION['mydata'] = $resultArray;
}