0

Background: I'm doing a little spare time job for a friend, making use of his RFID-reader and card.

Problem: The C part was cake, took no longer than an hour or two to set it up against a MySQL database. Problem is that he wants a web-based solution, and I'm dogsh*t with all web-based programming languages.

I need a script that updates another script, and I'm not sure how to go about it. I've tried Googling and searching this site, but I can't seem to quite understand the solutions posted.

This is the part which I want updated once a second or so. Overload due to many updates shouldn't become a problem. This is the sql-query:

$sql = "SELECT * FROM card_info ORDER BY datetime DESC LIMIT 1";

I've already set up code to run it when someone clicks a button, but I would like to run that query once a second or so so that it updates to the very latest card that was placed on the reader, in order to print it on the screen. Either that or have it run every time that table in the MySQL-database updates.

I'm simply not sure how to go about this, there are finished scripts which I don't quite understand how to point towards this script in particular. I doubt cronjobs are any use in this instance. Any ideas what-so-ever are much appreciated!

/Juni

Update: The code strip which I want to poke:

if(isset($_GET['fetchid'])) {

    $sql = "SELECT * FROM card_info ORDER BY datetime DESC LIMIT 1";

    if($mysqli = connect_db()) {
        $result=$mysqli->query($sql);
    }

    if(mysqli_num_rows($result)>0){
        while($row = mysqli_fetch_array($result)) {
            echo "Current card id is: " . $row['id'] ."." ;
        }
    }
}

Which fetches the latest db-entry, and would be good to have updated in realtime.

j08691
  • 204,283
  • 31
  • 260
  • 272
Juni K
  • 13
  • 3
  • 1
    Please, provide some code. If it a webpage so you can use solution form http://stackoverflow.com/a/2787691/3837150 – S K Oct 09 '15 at 15:48
  • @skorchakov No, I wouldn't like the entire page to be reloaded.. There's an image or two so it would make it very obvious. Can I somehow call that on just this script by having the script on a separate .php page and including it on this page (index.php)? – Juni K Oct 09 '15 at 16:00
  • So you want an asynchronous call like ajax ? – frz3993 Oct 09 '15 at 16:02
  • @frz3993 Probably, yes. Thing is that I find it somewhat difficult to understand how to implement it, and point it right. – Juni K Oct 09 '15 at 16:04
  • You will need to use javascript at the client side to do that. Make a http request with ajax every few seconds (beware of the overhead) and it is not that 'realtime'. Or you can use bi-directional communication between server and client like long polling or websocket. – frz3993 Oct 09 '15 at 16:21
  • @frz3993 Javascript kinda makes a little more sense to me. I'll just give it another go – Juni K Oct 09 '15 at 16:38
  • Managed to create a javascript that refreshes only a certain id [document.getElementById('mysql')], in which I could store the php script I wanted to be updated. – Juni K Oct 09 '15 at 17:36

0 Answers0