0

I want to make a exercice's page (for learning) where every refresh push a random entry from database. The code WORKS fine in localhost but not in the server. In server, for some reason, it push every time the same entry.

$query="SELECT basics FROM exercices ORDER BY RAND() LIMIT 1";
$result=mysqli_query($dbc, $query) or die ("Error of query".mysqli_error($dbc));
$row=mysqli_fetch_array($result);
$exercice=$row['basics'];
DBclose($dbc);

Sequence of pages: mainpage -> exercices_page (where php&mysql push exercice from DB) submit value to -> update script that update student puntuation and turn back to exercice's page with 'header('Location...'). At exercice's page, it should refresh with a new random exercice. But every time it's the same.

I don't understand why it happens in server if works in local. I try with 3 browsers, and each display a different entry but not changes the result. By the way: update script also update and sum the exercices made in the student profile, but the browser display the same value until new login in the page.

I'm stuck because I don't know if the problem is program or same server configuration.

Butterfly
  • 2,408
  • 4
  • 27
  • 47
  • 2
    Random is just *that*. It could return the same entry *n* number of times. It could also be that there is only one entry in your database. – Jay Blanchard Oct 19 '15 at 13:40
  • http://stackoverflow.com/questions/11514682/mysql-rand-not-working-always-getting-same-result here its – Subin Thomas Oct 19 '15 at 13:45
  • Hi, Jay, thanks the comment. There are actually 5 entries for now. In local, the browser displays, at times the same one, but mostly a different one, but in server, it displays every time the same. – Daniel Cenoz Oct 19 '15 at 14:03
  • 1
    are you sure there's > 1 on the live server? – Prisoner Oct 19 '15 at 14:13
  • Try running the query at the MySQL command prompt, ie outside of your PHP program. That will help you eliminate it being a problem in your code. – Simba Oct 19 '15 at 14:20
  • Hi, Prisoner. I ran a 'while' that shows all 5 entries. The server db it's the same at my local. Tnx – Daniel Cenoz Oct 19 '15 at 14:20
  • Sure nothing's just caching the very first result you got? – Matt Gibson Oct 19 '15 at 14:21
  • Hi, Simba. Thanks for your suggestion, I don't think in it yet. I ran the SQL query in PHP My Admin, several times now and it returns mostly different entries, as I want. But the browser keep "storing" the same result in any way I don't understand... – Daniel Cenoz Oct 19 '15 at 14:26
  • Well, browsers are basically *meant* to store the results of a simple page GET, unless you've instructed them not to... Does it work if you add [headers that instruct the browser not to cache](http://stackoverflow.com/questions/13640109/how-to-prevent-browser-cache-for-php-site)? (Bear in mind you'll need to clear your cache first before checking!) – Matt Gibson Oct 19 '15 at 14:38
  • Hi, Ryan! The page have a "submit" button that send the information to another script that update the database and return to exercices with "header("Location")". I think that when it return, it's a refresh... in local, it works. I clear the cache, and receive a different entry, yes. – Daniel Cenoz Oct 19 '15 at 14:40
  • try this, hit the same url with this chunk at end: `http://myurl.com/theThing.php&magicnumber=8` ... it is a common cache issue, http://stackoverflow.com/a/30540995/1816093 – Drew Oct 19 '15 at 14:43
  • if you would load fiddler from telerik (or charles), you would find that the browser is fetching from browser cache, not from the server – Drew Oct 19 '15 at 14:47
  • I'm trying your sugestions, tnx! I just try another pc with the same result. If I close the browser and re-enter the page, I have a different result but it don't change anymore by refreshing only. – Daniel Cenoz Oct 19 '15 at 15:00
  • I will try to clean the cache with header but I don't know where to put the code:
    – Daniel Cenoz Oct 19 '15 at 15:04
  • I just tried the location option and have the same problem. Works ok on local but no in server... And I don't understand the Drew's sugestion... if I paste the &magicnumber=837493 sequence I have a 404... It's that? – Daniel Cenoz Oct 19 '15 at 16:13
  • You can output the cache control headers at any point in your PHP script before you start sending the actual page (`header()` adds an [HTTP header](http://code.tutsplus.com/tutorials/http-headers-for-dummies--net-8039) to the output, so it's got to come before the HTTP body, i.e. the actual web page.). People typically put the code right at the start of their PHP. – Matt Gibson Oct 19 '15 at 16:15
  • Hi, Matt. I put the cache control header at the very begining of the page, and have no difference in the output from db (but i can see a little "blink" when the css it's readed from, i think). – Daniel Cenoz Oct 19 '15 at 16:19
  • And you've definitely cleared the cache on your browser? Don't forget, the problem is that it's using the old page, so unless you clear your cache your new version with the cache-control headers won't necessarily be loaded! – Matt Gibson Oct 19 '15 at 16:49
  • I totally clear the chrome's cache via configuration reset. Close the browser and return to the page. The first time, the script push random entry and correct profile's points. I submit the form (number of corrects answers to profile) and the browser refresh same old entry and profile's points. But I can see in phpmyadmin the correct points... – Daniel Cenoz Oct 19 '15 at 17:49
  • I tried to refresh cache via header but nothing new happens... I tried use rand() to generate a random number outside the query. but the same... any idea? I don't understood Drew's sugestion. – Daniel Cenoz Oct 20 '15 at 23:15

0 Answers0