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

Summary

I have created a PHP page that displays data from a MySQL table. 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.

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. I have already implemented one ajax feature, which allows the user to type into a textbox the number of results they wish to see. I have then called the AJAX function using the onkeyup="LOADXMLDoc()". (LOADXMLDoc() is the function that handles all the 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. The part I am having difficulty with is the algorithm needed to retrieve only the most recent data.

Files

  • Displaydata.php

    Contains the page that the user sees, with the textbox, and the window to view data.

  • getData.php

    Contains the code to connect to the database, and query for results according to user specification.

  • 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:

  • Loop the function() call in the displaydata.php

    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

    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

    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. Because I want to be able to use it in 2 of the files, I am storing it in the $_SESSION array. I declare the variable in the displaydata.php file, and want to access it in the 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"); I then try and access this in the getRecentData.php file using: $currentHighest = $_SESSION['currentHighest'];

As a test, I then use: 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. 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; again, and the $currentHighest variable. 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. 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. 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

You would be better off using websockets if you want immediate updates of the data in the browser. Rather than querying the database, parse the UDP results and send those results to the socket. This library is a good starting point - http://socketo.me/

You can run the PHP script that listens for the UDP packets as a service. (for example - using UpStart on Ubuntu).