Home > other >  Send the next Row if we skip the previous Row in DB
Send the next Row if we skip the previous Row in DB

Time:11-03

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?

CodePudding user response:

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.

CodePudding user response:

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;

}

  • Related