简体   繁体   English

使用AJAX从MySQL数据库更新PHP页面:用于计算最近添加的数据的算法

[英]Updating PHP page from MySQL database using AJAX: Algorithm to calculate recently added data

Summary 摘要

I have created a PHP page that displays data from a MySQL table. 我创建了一个PHP页面,该页面显示MySQL表中的数据。 This table is having data automatically inserted into it through a python udp listener I wrote, and a bash script that broadcasts data every 20 seconds. 该表通过我编写的python udp侦听器自动将数据插入其中,并且bash脚本每20秒广播一次数据。

I want to use AJAX to display new results whenever they are inserted into the database, without the user ever needing to refresh the page. 每当将新结果插入数据库时​​,我都想使用AJAX来显示新结果,而用户则无需刷新页面。 I have already implemented one ajax feature, which allows the user to type into a textbox the number of results they wish to see. 我已经实现了一个ajax功能,该功能允许用户在文本框中键入他们希望看到的结果数。 I have then called the AJAX function using the onkeyup="LOADXMLDoc()" . 然后,我使用onkeyup="LOADXMLDoc()"调用了AJAX函数。 (LOADXMLDoc() is the function that handles all the AJAX) (LOADXMLDoc()是处理所有AJAX的函数)

The code I provide is in no language specifically, it's just a way to get the idea across. 我提供的代码没有特定的语言,这只是一种传达想法的方法。

The Goal 目标

The ultimate aim here is to have a PHP page displaying real time data from a MySQL database. 这里的最终目的是要有一个PHP页面来显示来自MySQL数据库的实时数据。 The part I am having difficulty with is the algorithm needed to retrieve only the most recent data. 我遇到的困难是仅检索最新数据所需的算法

Files

  • Displaydata.php Displaydata.php

    Contains the page that the user sees, with the textbox, and the window to view data. 包含用户看到的页面,文本框和查看数据的窗口。

  • getData.php 访问getdata.php

    Contains the code to connect to the database, and query for results according to user specification. 包含用于连接到数据库并根据用户规范查询结果的代码。

  • getRecentData.php getRecentData.php

    This is where the algorithm I need to fetch the most recent data will be. 这是我需要提取最新数据的算法。

The Algorithm 算法

I need the algorithm to: 我需要该算法来:

  • Scan the database every second(for example). 每秒扫描数据库(例如)。
  • Calculate whether any new files have been added. 计算是否已添加任何新文件。
  • Update the web page with any new files, but only new data. 使用任何新文件( 新数据)更新网页。 I don't want unnecessary data being uploaded onto the page. 我不想将不必要的数据上传到页面上。

What I have so far 到目前为止我有什么

Repeated Scan 重复扫描

In order for the scanning of the database, I came up with 3 methods: 为了扫描数据库,我想出了3种方法:

  • Loop the function() call in the displaydata.php displaydata.php中循环function()调用

     Do while BooleanVariable = True { <?php sleep(1); ?> LOADXMLDoc2(); //The LOADXMLDoc2() is just the same as the previous one but for this AJAX request, just an example name for now. } 

    Personally I find this method quite messy, and I don't think it will work. 我个人觉得这种方法很混乱,我认为它不会起作用。

  • When the function is called, loop the entire code in the GetRecentData.php 调用该函数时,在GetRecentData.php中循环整个代码

     Do while BooleanVariable = True { sleep(1); //Execute the algorithm to fetch recent data. } 

    I find this a cleaner method, but would this continue to run if the function is only called once? 我发现这是一种更干净的方法,但是如果该函数仅被调用一次,这种方法是否会继续运行?

  • The final method is to use the javascript SetTimeout() function 最后一种方法是使用javascript SetTimeout()函数

     Do while BooleanVariable = True { SetTimeout(LOADXMLDoc2(), 1000) //Run function every second } 

    To me, this seems like the cleanest method so far, but I'm open to any other solutions. 对我来说,这似乎是迄今为止最干净的方法,但是我可以接受任何其他解决方案。

Calculating existence of new records 计算新记录的存在

I began by creating a variable called $currentHighest, rather self explanatory, I want this variable to contain the value of the largest id integer that is currently used. 我首先创建一个名为$ currentHighest的变量,这很容易解释,我希望该变量包含当前使用的最大id整数的值。 Because I want to be able to use it in 2 of the files, I am storing it in the $_SESSION array. 因为我希望能够在两个文件中使用它,所以将其存储在$ _SESSION数组中。 I declare the variable in the displaydata.php file, and want to access it in the getRecentData.php . 我在displaydata.php文件中声明了变量,并希望在getRecentData.php中访问它。

I call the session_start() function at the top of each page, and declare the variable using the following: $_SESSION['currentHighest'] = mysql_query("SELECT MAX(id) FROM data"); 我在每个页面的顶部调用session_start()函数,并使用以下命令声明变量: $_SESSION['currentHighest'] = mysql_query("SELECT MAX(id) FROM data"); I then try and access this in the getRecentData.php file using: $currentHighest = $_SESSION['currentHighest']; 然后,我尝试使用以下方法在getRecentData.php文件中访问它: $currentHighest = $_SESSION['currentHighest'];

As a test, I then use: echo $currentHighest; 作为测试,我然后使用: echo $currentHighest;

Theoretically, I should think that would print out the value of the largest id that has been used, but currently, nothing is being printed. 从理论上讲,我应该认为这将打印出已使用的最大id的值,但目前没有任何打印内容。 This then forms the start of the algorithm. 然后,这构成了算法的开始。

Now I need to calculate the number of records that are new, using the SELECT MAX(id) FROM data; 现在,我需要使用SELECT MAX(id) FROM data;来计算新的记录数SELECT MAX(id) FROM data; again, and the $currentHighest variable. 再次,和$ currentHighest变量。 My current method looks a little like this: 我当前的方法看起来像这样:

var x = 0;
var NewMax = ("SELECT MAX(id) FROM data");
Do until newMAX(id) = currentHighest {
  sleep(1000);
  NewMax = NewMax - x
  x += 1 
}

So this recursively takes off an incrementing "x" until the NewMax = current ID. 因此,它递归地取一个递增的“ x”,直到NewMax =当前ID。 I then need some way of getting the number of records that has found and running another MySQL query to get the new records and insert them into the page. 然后,我需要某种方式来获取已找到的记录数,并运行另一个MySQL查询以获取新记录并将其插入页面。 The inserting into the page isn't a problem, I have already done that once. 插入页面不是问题,我已经完成了一次。

The solution? 解决方案?

What I have described is just my initial idea of how to achieve this task. 我所描述的只是我如何实现此任务的最初想法。 I am looking for either, if this seems a reasonable way of achieving my goal, how do I complete it? 我正在寻找一种方法,如果这看起来是实现目标的合理方法,我该如何完成? Or, what would be a better way of achieving the goal. 或者,什么是实现目标的更好方法。 I am not looking for specific code, unless it helps the explanation, but instead the theory of how to achieve the goal. 我不是在寻找特定的代码,除非它有助于解释,而是在寻找实现目标的理论。 I can find the code online. 我可以在网上找到代码。 If people down-vote, can you leave a reason in the comments, so I can improve my question writing. 如果人们不赞成,可以在评论中留下原因,以便我改善问题写作。 Thanks in advance. 提前致谢。

If you wish to view any of the specific code I have: 如果您想查看任何特定的代码,我都有:

https://gist.github.com/edprince/e3af255d57283b4f820e https://gist.github.com/edprince/e3af255d57283b4f820e

You would be better off using websockets if you want immediate updates of the data in the browser. 如果要立即更新浏览器中的数据,最好使用websockets。 Rather than querying the database, parse the UDP results and send those results to the socket. 解析UDP结果并将这些结果发送到套接字,而不是查询数据库。 This library is a good starting point - http://socketo.me/ 该库是一个很好的起点-http: //socketo.me/

You can run the PHP script that listens for the UDP packets as a service. 您可以运行将UDP数据包作为服务侦听的PHP脚本。 (for example - using UpStart on Ubuntu). (例如-在Ubuntu上使用UpStart)。

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

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