skip to Main Content

I have a php script that runs every 20 minutes (I use screen on Debian – server side),

The php script selects the first row of the Database using LIMIT 1, like:

$q = mysqli_query($connection,"SELECT * FROM table_name LIMIT 1");

Once selected the first Row it will send the result to a telegram bot, delete the 1st row from the DB and the scripts ends.

It repeats this process every 20 minutes cause the screen.

Now, the question is, if the first row I got with LIMIT 1 doesn’t meet the criteria (at the moment it is deleted and skip the actual process until the next 20 minutes), how can I make it to select the 2nd row (which is now the 1st row again) to get the new data using the same script and avoiding wait the next 20 minutes?
Is it possible with my actual script LIMIT 1?

2

Answers


  1. Chosen as BEST ANSWER

    Thanks for replying back!

    I think I can use your suggestion but I need to edit it, the check I need to perform is as follows:

    $q = mysqli_query($connection,"SELECT * FROM table LIMIT 1");
    $r = mysqli_fetch_assoc($q);
    
        $e = $r['id'];
        $asin_queue = $r['asin'];
        $price_queue = $r['new_price'];
    

    //perform the check to see if the price has changed

    $r = file_get_contents("https://api.keepa.com/product?asin=$asin_queue&key=$api_keepa");
    
        $gz = gzdecode($r);
    
        $t = json_decode($gz);
    
        //price updated
        $new_price = $t->products[0]->csv;
    

    //here is where I am stuck I should do something like this:

    while($new_price > $price_queue){
    

    *// It will need to delete the first row on the DB and do the check again, until it get the right condition $new_price <= $price_queue and then it can quit the while loop.

    //repeat the above to do the check again*

    $q = mysqli_query($connection,"SELECT * FROM table LIMIT 1");
    
    $r = mysqli_fetch_assoc($q);
    
        $e = $r['id'];
        $asin_queue = $r['asin'];
        $price_queue = $r['new_price'];
    

    //perform the check to see if the price has changed

    $r = file_get_contents("https://api.keepa.com/product?asin=$asin_queue&key=$api_keepa");
    
        $gz = gzdecode($r);
        $t = json_decode($gz);
    
        //price updated
        $new_price = $t->products[0]->csv;
    

    }


  2. Obviously I don’t have access to your database, and I don’t know what your criteria are, so this will all be pseudocode. I am assuming you will want to delete the first row if it doesn’t match the criteria, but it would be just as possible to only skip it:

    $finished = false;
    
    while(!$finished) {
      
      // you already have the code to get the first row.  It goes here.
      // I will assume those results are in $row
      
      $finished = $row MEETS CRITERIA; // whatever these criteria are
      
      // you also have the code to delete the current row.  put that here
      // whether this row satisfies the condition or not.  The row you 
      // just selected will always be deleted
      
    }
    

    It’s as simple as that. If the row met the criteria, then $finished is TRUE and the while loop will terminate*. If the row didn’t meet the criteria, the $finished is FALSE and the while loop will run again. It will run until a row meets the criteria and $finished is TRUE.

    * ! is the php NOT operator. It inverts the value. So basically what the while loop is saying is this:
    while not finished { do this code }

    Also, to avoid a non-terminating loop, you’ll need to make sure that your result set has something in it, or else $finished will never be set to TRUE. I’m not sure how you’re executing, so I’m not going to suggest an implementation. Just be aware that it is possible for a while to become a neverending loop and take measures to avoid it. For instance, you might:

    $finished = false;
    $retries = 5;
    
    while(!$finished && $retries-->0) {
      
      //the code
      
    }
    

    This decrements $retries every loop, and once it gets to 0 the while will stop no matter what. It’s a good idea to put a failsafe in any loop that might accidentally (or unforeseenedly) become unterminated, especially during development, so you don’t hang your code.


    EDITED now that I have seen some of the sample code:

    $finished = false;
    $retries = 5;
    
    while(!$finished && $retries-->0) {
    
      $q = mysqli_query($connection, "SELECT * FROM table LIMIT 1");
      $r = mysqli_fetch_assoc($q);
    
      $e = $r['id'];
      $asin_queue = $r['asin'];
      $price_queue = $r['new_price'];
      
      $r = file_get_contents("https://api.keepa.com/product?asin=$asin_queue&key=$api_keepa");
      $gz = gzdecode($r);
      $t = json_decode($gz);
      $new_price = $t->products[0]->csv;  
      
      // put any other stuff you need to do here, 
      // like updating any db entries, etc
    
      // you also have the code to delete the current row.  put that here
      // whether this row satisfies the condition or not.  The row you 
      // just selected will always be deleted
    
      $finished = $new_price <= $price_queue;
    
    }
    

    And that’s pretty much it. It’s your existing code, just wrapped in a while block so that it loops. The only thing I’ve added is a line to check for a condition inside the while block so that the code knows when to exit.

    Also, let’s look at this: $retries-->0, because that line may be a little confusing. So I said you needed some kind of failsafe to keep your code from accidentally looping forever if you make a mistake or oversight, and so I assign a variable $retries and make it equal to 5. — is php shorthand for "subtract one from this", so $retries– just subtracts one from whatever the value is currently*. >0 is just a value check, even though I wrote it so it all runs together, it’s two separate things. It could have been written like this: $retries-- > 0.

    In context of the while loop, there’s a few things happening:

    while( !$finished && $retries-->0 ) {}
    

    and the whole thing has to evaluate to TRUE for the loop to keep running. && is the logical operator AND. ! is the logical operator NOT. So in plain language terms that condition would read:

    "while not finished and the number of remaining retries is greater than 0, continue looping."

    Hope all this helps.


    * — can go before or after the variable it’s modifying. If it goes after ($retries--) then the decrement doesn’t take effect immediately. This lets you use the variable in a logical test before the decrement is applied. 5– > 4 is true. If you put the — before the variable, the decrement happens first. –5 > 4 would be false, because the subtraction will happen before the comparison.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search