Jump to content

Random query always returns same result


Raconteur

Recommended Posts

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
Link to comment
Share on other sites

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!
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.