1

I'm a newbie, so be kind :)

What is the simplest solution for checking for, retrieving and using newly received data from a mysql database?

The database is being updated from an external api. Ideally I would get it as it arrives but using intervals could work too. On the first iteration and then once every 5 minutes if there is no new data meanwhile - do an action with the next entry of the array which might cause data to be updated within the next 5 min. But I want to make sure to stop everything if the new data has been received, and just perform some stuff in php.

what's the most simple solution? php vs jquery/ajax?

my proposed solution is in jquery:

        <script>
            var newdata = false;
            if(newdata === false){
                setTimeout (function(){
                    $.each(array, function(){
                            $.post('checkdb.php',data,function(resp){
                                          if(resp){
                                          newdata=resp;
                                          return newdata;                                                  
                                          }
                                          else{
                                          $.post('doaction.php',data);
                                          // cause a potential update within the next 5 min
                                          }
                            });
                    });
                }, 300000); 
            }
            else{
                // move newdata back to php and (then) do something with response
            }
        </script>

Thanks! :)

Lucy Weatherford
  • 5,452
  • 16
  • 50
  • 76
  • 1
    You should not put semicolons after blocks. It doesn't make sense to wrap everything in a PHP tags, either (JavaScript is not PHP at all). – pimvdb Dec 23 '11 at 23:01
  • Ok thanks! will fix the ;. Regarding the php tag: I know, but this is part of a php page (that's where all the variables come from), if you can suggest a pure php solution that's great too. tnx – Lucy Weatherford Dec 23 '11 at 23:05
  • okay, i fixed both, tnx! any comments on the logic would be appreciated too :) – Lucy Weatherford Dec 23 '11 at 23:34

2 Answers2

1

My solution in the end is indeed in php and not ajax (there is really no need for client side in this situation).

Here's the outline:

    <?php
        while (something is true){
            //do stuff

            flush(); // execute the stuff you did until now
            sleep(300); // wait 5 min
            // now check database and retrieve new data, if any, and insert into $result 

            if (isset($result)){
                //do stuff with the $result
                break; // get out of the loop
            }
        }
    ?>
Lucy Weatherford
  • 5,452
  • 16
  • 50
  • 76
0

Don't use such logic on client side. JavaScript could be easily manipulated/changed. Someone could replace the interval with much shorter one and stress the server. Read about Comet, use it for waiting for the results from the server. On the server side do all the logic: implement database polling (with interval, as you wish), and, when needed, push the data to the browser/stop polling/do whatever you want.

jacek
  • 947
  • 1
  • 11
  • 20
  • okay this sounds like the direction i should take, thanks. but how do i use intervals in php? i couldn't figure it out – Lucy Weatherford Dec 23 '11 at 23:52
  • Well it depends of course what really you are trying to achieve. You can use PHP sleep() function to trigger action in a loop within the request timeout. When the timeout is reached, JavaScript should initiate another request. You can also read about PHP sockets. – jacek Dec 24 '11 at 00:08
  • Also, you can use a comet server or node.js. Lots of possibilities. – jacek Dec 24 '11 at 00:21
  • in php it is by using "sleep"? but why the js at all? as far as i can see, the only reason i am using js, is for the timeout function. all the rest is basically logic+variables from php. so if the time would be in php too, then is there still need for js here? – Lucy Weatherford Dec 24 '11 at 00:43
  • Yes, because still something has to pull the data from the server. PHP on its own can't send anything. You have to request it - either by using refresh button in the browser, or AJAX/Comet. – jacek Dec 28 '11 at 22:58