title: Randomised content from PHP script not refreshing on reload
body: <p>I am trying to implement a site which displays a random image every time the page is refreshed.</p>
  
  <p>Currently, all the images are in a directory "/pngs/", and I have a MySQL database with indexed rows:</p>
  
  <pre><code>&lt;img src="pngs/000.png"&gt;
  &lt;img src="pngs/001.png"&gt;
  &lt;img src="pngs/002.png"&gt;
  &lt;img src="pngs/003.png"&gt;
  ...
  </code></pre>
  
  <p>Then, in the HTML:</p>
  
  <pre><code>&lt;div class='img'&gt;&lt;?php include('pngs.php');?&gt;&lt;/div&gt;
  </code></pre>
  
  <p>Referring to the PHP script, which selects a random row from the database:</p>
  
  <pre><code>&lt;?php
    require_once 'login.php';
    $conn = new mysqli($hn, $un, $pw, $db);
    if ($conn-&gt;connect_error) die($conn-&gt;connect_error);
    $query  = "SELECT * FROM pngs";
    $result = $conn-&gt;query($query);
    if (!$result) die($conn-&gt;error);
    $rows = $result-&gt;num_rows;
    $r = rand(1,$rows);
    $result-&gt;data_seek($r);
    $row = $result-&gt;fetch_array(MYSQLI_ASSOC);
    echo $row['png'];
    $result-&gt;close();
    $conn-&gt;close();
  ?&gt;
  </code></pre>
  
  <p>At the moment, this works perfectly cross-browser when run from a local Apache Web Server. However when uploaded and run from the actual site, the images remain fixed on refresh. </p>
  
  <p>Clearing the cache and refreshing produces the desired effect, so this is perhaps an issue with cache? I have tried various cache clearing and no-cache methods, none of which have worked.</p>
  
  <p>One thought is that perhaps the PHP script is only run once then its results stored in cache? In which case how would I force the PHP script to run each refresh?</p>
  
  <p>Another thought is that perhaps using Javascript to display a random image would be an easier and better solution? Willing to adapt if that is so.</p>
  
  <p>Would really appreciate any advice, relative newbie here.</p>
  
  <p>Thanks.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Thanks for your replies, the image cache workaround solves the issue for images.</p>
  
  <p>However the initial problem is more general and occurs similarly with text.</p>
  
  <p>For example, if I use a MySQL database with indexed rows of text:</p>
  
  <pre><code>"First text to be displayed."
  "Second text to be displayed."
  "Third text to be displayed."
  "Fourth text to be displayed."
  </code></pre>
  
  <p>And if I use a similar PHP script as shown above, the text displayed is correctly randomized initially, but then remains the same when the page is refreshed.</p>
  
  <p>So the intended question is more general: how do I ensure a PHP/MySQL query is executed on every refresh, rather than the first result cached and repeated?</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>Apologies for the further edit, not sure whether to start a new question for this now...</p>
  
  <p>So it seems like I may have misunderstood how the PHP/MySQL infrastructure would work...</p>
  
  <p><em>Intended functionality</em>: Each page view returns a different result from the MySQL database, resulting in different images/text for each user on every refresh.</p>
  
  <p><em>Current functionality</em>: Each page view returns the same results from the MySQL database, across various users. The results displayed are random, and do update, but every few minutes instead of every refresh. Results displayed are the same across users, implying that the PHP script / MySQL query is happening server-side, rather than uniquely for each user.</p>
  
  <p>Thinking that I may have decided to use the wrong infrastructure for the intended result, now considering trying an implementation in javascript.</p>