dongyun3805 2014-11-13 06:05 采纳率: 0%
浏览 24
已采纳

记住在PHP页面中使用的MYSQL查询结果

I have a PHP page set up to generate a rather large set of data generated from MYSQL queries.

Using this data it will create a certain number of table headers (in html ) dependant on the number of users currently in the system.

foreach($usersFromMYSQL as $row)
{
    echo
    "
    <th>$someUserData
    <th>Col Y
    <th>Col Z
    ";
}    

It will then also populate the table with a certain of number of rows (in html ) dependant on the number of events occurring in the system

foreach ($eventsFromMYSQL as $row)
{
    echo
    "
    <tr>
    ";

    foreach($usersfromMYSQL as $inner_row)
    {
        echo
        "
        <td>$someUserSpecificEventData1
        <td>$someUserSpecificEventData2
        <td>$someUserSpecificEventData3
        ";
    }
}

This code is heavily simplified, but identical in format, to my website.

My problem is that I am running my website on a Raspberry PI, and the load times for this page are (expectedly) slow due to the number of users and events in the database. Currently each time you access the page all of this data is requested again, and there is (to my knowledge at least) no form of caching or memorisation involved.

The data can possibly change day by day, meaning that if the page where to be cached, I would want it to only remain cached for the remainder of that day, as the next day could possibly have different results.

My question is what solutions exist to prevent this data being reloaded every time the page is visisted, but ensure that it is at least reloaded once a day?

  • 写回答

1条回答 默认 最新

  • dongsigan2636 2014-11-13 06:15
    关注

    I assume you use MVC system, then it might be done like this :

    1. In your controller, check whether the view page for that day is exists or not.
    2. If not, then get all query and generate all HTML data and then save it as a view file by using unique file name (ex. 2014_01_01.php). After that, load the page.
    3. If exists, direct load the view page for that day.

    Please pay attention that once the page is generated for current date, the script should not generate page again.

    EDIT 1

    If you are using single file,

    <?php 
         $file_name = date('d_m_Y').".php";
         if(file_exists($file_name))
         {
              //load 
              $page_data = file_get_contents($file_name);
         }
         else
         {
              //generate page here
              //
              $page_data = "....... YOUR HTML PAGE DATA HERE ........ ";
              //save it
              file_put_contents(date('d_m_Y').".php");
         }
         //show page to user
         echo $page_data;
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入