Raconteur Posted November 22, 2006 Share Posted November 22, 2006 Hi gang,I am either a bumbling idiot or am doing something so simply wrong that I just cannot see it...I have a script (PHP) that is using an AJAX call to pull a random row out of the DB, using:[code]SELECT * FROM <table> WHERE active=1 ORDER BY rand() LIMIT 1[/code]If I run that in a query editor against the DB it works as expected. A different, random result on each call.However, when I run it from my code, I ALWAYS get the same result. The AJAX calls a server-side script which looks like this:[code]include_once("ssDataBroker.php"); $ssDB = new SSDataBroker();$ss = $ssDB->retrieveRandomSS(); $ssString = implode("~", $ss);echo $ssString;[/code]The code in the SSDataBroker object for #retrieveRandomSS() looks like this:[code]$sql = "SELECT * FROM aia_cms_success_stories WHERE active=1 ORDER BY rand() LIMIT 1";$results = MYSQL_UNBUFFERED_QUERY($sql);return MYSQL_FETCH_ARRAY($results, MYSQL_ASSOC);[/code]In this code I have tried using MYSQL_QUERY, and thought that the result was being cached, so I switched to MYSQL_UNBUFFERED_QUERY, but that did not help. I have also tried closing the connection to the DB and reopening it on every call, but that did not help either.The problem seems to be in the PHP code itself, since the query works just fine when run against the DB, but I cannot see what it wrong.Can someone out there help?Thanks in advance!Chris Quote Link to comment https://forums.phpfreaks.com/topic/28130-random-query-always-returns-same-result/ Share on other sites More sharing options...
Vikas Jayna Posted November 22, 2006 Share Posted November 22, 2006 The php code looks fine, would require a bit of hit and trial to get this right. I wonder whether the output of the script is getting cached in the browser. The same can be found by executing the php script (which is being called through ajax) from the command line, like this:[b]php -q <scriptname>[/b]Try this a few times and if this gives different results, the point is proven. Caching of php output can be stopped by using the header function like this:-[code]header("Cache-Control: no-cache, no-store, max-age=0, must-revalidate");[/code]Hope this solves the problem! Quote Link to comment https://forums.phpfreaks.com/topic/28130-random-query-always-returns-same-result/#findComment-128670 Share on other sites More sharing options...
Raconteur Posted November 22, 2006 Author Share Posted November 22, 2006 Hi Vikas,Thanks for the info. Seems easy to just add the code and see if the problem is fixed. Can it be added to the HTML HEAD directly? I already have:[code]<META HTTP-EQUIV="Pragma" CONTENT="no-cache"><META HTTP-EQUIV="Expires" CONTENT="-1">[/code]Will that not work?Cheers,Chris Quote Link to comment https://forums.phpfreaks.com/topic/28130-random-query-always-returns-same-result/#findComment-128694 Share on other sites More sharing options...
Raconteur Posted November 22, 2006 Author Share Posted November 22, 2006 Ok, so testing the PHP script from the command line seems to work just fine, so it is the browser caching the result.Now my problem is, the AJAX code is being call to populate a little DIV in a small box on the main page, so how do I install additional headers if the page already exists? That is the part I am not getting.Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/28130-random-query-always-returns-same-result/#findComment-128806 Share on other sites More sharing options...
Vikas Jayna Posted November 23, 2006 Share Posted November 23, 2006 Hi Raconteur,Use the header() function in your php scripts that sends the database record as the output . This will ensure that it does not get cached in the browser and whenever the script is called through ajax it will give different output Quote Link to comment https://forums.phpfreaks.com/topic/28130-random-query-always-returns-same-result/#findComment-129018 Share on other sites More sharing options...
Raconteur Posted November 23, 2006 Author Share Posted November 23, 2006 Gotcha,Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/28130-random-query-always-returns-same-result/#findComment-129192 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.