title: Stopping a PHP loop when updating MySQL database
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>PHP:</p>
  
  <pre><code>&lt;?php
    $conn = mysqli_connect("localhost", "root", "", "prices");
    if ($conn-&gt;connect_error)
    {
      die("Connection error: ". $conn-&gt;connect_error);
    }
    $result = $conn-&gt;query("SELECT `price` FROM `priceTable` WHERE `company` = 'Bawden'");
    $x = 0;
      if ($result-&gt;num_rows &gt; 0)
      {
        while ($row = $result-&gt;fetch_assoc())
        {
          echo $row['price'];
          echo '&lt;br&gt;&lt;br&gt;';
          echo $x;
          if ($x &lt; 5)
          {
            $random = (rand(3300, 3700) / 100);
            $sql = $conn-&gt;query("UPDATE `priceTABLE` SET `price` = '$random' WHERE `company` = 'Bawden'");
            $x++;
          }
        }
      }
  ?&gt;
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>However, my actual result is that the website and database are both continuously being updated, not stopping after five times.</p>