1

I am trying to implement a site which displays a random image every time the page is refreshed.

Currently, all the images are in a directory "/pngs/", and I have a MySQL database with indexed rows:

<img src="pngs/000.png">
<img src="pngs/001.png">
<img src="pngs/002.png">
<img src="pngs/003.png">
...

Then, in the HTML:

<div class='img'><?php include('pngs.php');?></div>

Referring to the PHP script, which selects a random row from the database:

<?php
  require_once 'login.php';
  $conn = new mysqli($hn, $un, $pw, $db);
  if ($conn->connect_error) die($conn->connect_error);
  $query  = "SELECT * FROM pngs";
  $result = $conn->query($query);
  if (!$result) die($conn->error);
  $rows = $result->num_rows;
  $r = rand(1,$rows);
  $result->data_seek($r);
  $row = $result->fetch_array(MYSQLI_ASSOC);
  echo $row['png'];
  $result->close();
  $conn->close();
?>

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.

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.

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?

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.

Would really appreciate any advice, relative newbie here.

Thanks.

EDIT

Thanks for your replies, the image cache workaround solves the issue for images.

However the initial problem is more general and occurs similarly with text.

For example, if I use a MySQL database with indexed rows of text:

"First text to be displayed."
"Second text to be displayed."
"Third text to be displayed."
"Fourth text to be displayed."

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.

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?

EDIT 2

Apologies for the further edit, not sure whether to start a new question for this now...

So it seems like I may have misunderstood how the PHP/MySQL infrastructure would work...

Intended functionality: Each page view returns a different result from the MySQL database, resulting in different images/text for each user on every refresh.

Current functionality: 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.

Thinking that I may have decided to use the wrong infrastructure for the intended result, now considering trying an implementation in javascript.

  • as facts can be alternative, I guess solutions too :) check this [SO answer](http://stackoverflow.com/questions/16428154/javascript-random-image-selected-on-refresh). I would prefer anything that doesn't hit the DB when not needed, but if user disable js then you're in trouble... – OldPadawan Apr 07 '17 at 11:20
  • To minimize the stress on the database, I would generate a random number in PHP first, and then only load the one specific image needed from the database. Use the trick in the link above to prevent caching. – glaux Apr 07 '17 at 11:22

1 Answers1

0

You can do mysql query itself, by executing ORDER BY RAND()

SELECT * FROM  `pngs` ORDER BY RAND( );

This will execute query and produce result randomnly row results

Gopalakrishnan
  • 913
  • 8
  • 17