简体   繁体   中英

Stopping a PHP loop when updating MySQL database

I'm creating a basic main menu for a stock market simulator where the price of a company will be updated periodically. For testing purposes, I need to make a loop to display the price of a share on the website five times (with the website automatically updating without refreshing) and to update the database at the same time.

I have successfully wrote some code which will both update the database with the current share price and will also update the website as well. However, when I have tried to include a loop I have come to a problem. I have included a loop to iterate five times but the problem that I am having is that the code continues to iterate even after five tries.

PHP:

<?php
  $conn = mysqli_connect("localhost", "root", "", "prices");
  if ($conn->connect_error)
  {
    die("Connection error: ". $conn->connect_error);
  }
  $result = $conn->query("SELECT `price` FROM `priceTable` WHERE `company` = 'Bawden'");
  $x = 0;
    if ($result->num_rows > 0)
    {
      while ($row = $result->fetch_assoc())
      {
        echo $row['price'];
        echo '<br><br>';
        echo $x;
        if ($x < 5)
        {
          $random = (rand(3300, 3700) / 100);
          $sql = $conn->query("UPDATE `priceTABLE` SET `price` = '$random' WHERE `company` = 'Bawden'");
          $x++;
        }
      }
    }
?>

The above code will be displayed in a separate document with Javascript code and I can post this if required in the original post however I originally chose not to as I believe this is a PHP only problem. I have chosen to display $x to see if the value will increment. However, when running, the value of $x will stay at 0.

My expected result is that, on the website, there will only be five different updates and in the database, the database will only be updated five times.

However, my actual result is that the website and database are both continuously being updated, not stopping after five times.

What makes you think the loop should stop after 5 iterations?

You need to add the condition $x<5 in the while ($row = $result->fetch_assoc())

Edit following your comment

What you initially wrote is something like loop hundreds of times if need be and do something in the first 5 occurrences (starting loop 6, keep looping but do nothing) .

Now for the 2nd half of your comment, I'm not sure what you mean.
What I see in your code is:

  1. Select all prices for company = 'Bawden'
  2. Update all the prices for company = 'Bawden' 5 times (loop) with a random value, the same one, on all the records.

Not enough information to tell for sure but I don't think it makes sense: on one hand, you except to have several records under company = 'Bawden (= reason why you created a loop), on the other hand, your update feels like it is written under the assumption there would be 1 record only...
Are you missing something like a price date from your table? What is the primary key of priceTable ?

Try to post more technical details about your table (definition, sample of data) or it will be complicated to help further.

  The else will break the first loop, the second one will stop on the first while loop.



while ($row = $result->fetch_assoc())
              {
                echo $row['price'];
                echo '<br><br>';
                echo $x;
                if ($x < 5)
                {
                  $random = (rand(3300, 3700) / 100);
                  $sql = $conn->query("UPDATE `priceTABLE` SET `price` = '$random' WHERE `company` = 'Bawden'");
                  $x++;
                }else{
    break;
    }
    break;
              }

I'm trying to limit the update command to only 5 updates yes. At the moment, for testing purposes, there is only one company in my database with one price only. So I'm updating this one company's price five times

If you need to do the update 5 times for each row returned from the database, change your if statement to a for loop. Change this :-

 if ($x < 5)
    {
      $random = (rand(3300, 3700) / 100);
      $sql = $conn->query("UPDATE `priceTABLE` SET `price` = '$random' WHERE `company` = 'Bawden'");
      $x++;
    }

to this

for ($x = 0, $x < 5, $x++)
{
      $random = (rand(3300, 3700) / 100);
      $sql = $conn->query("UPDATE `priceTABLE` SET `price` = '$random' WHERE `company` = 'Bawden'");
}

This will repeat the process exactly 5 times and not rely on a separate counter (remove the other references to $x). Not sure why you would want to update the same record 5 times with different random values though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM