Jump to content

Archived

This topic is now archived and is closed to further replies.

masgas

stop repeating question

Recommended Posts

Hi I've got this code for selecting a number of questions from a db to make a test. It's random and sometimes it selects the same questions and they appear twice in the tests... how could I stop this from happening?

$sql = "SELECT * FROM questions ORDER BY RAND() LIMIT 20";

thanks in advance!

Share this post


Link to post
Share on other sites
That's impossible -- there's absolutely no way that the the statement posted above can ever return the same row twice. That's not to say that you didn't put the same question in the table itself twice -- which seems to be the case if you're getting duplicates.

Share this post


Link to post
Share on other sites
I don't know why it did so... but I assure you it did! Though I found the way round it like this...

$sql = "SELECT DISTINCT *, id FROM questions ORDER BY RAND() LIMIT 20";

regards...

Share this post


Link to post
Share on other sites
The fact that you had to use DISTINCT proves that you had the same data in two separate records -- and that should NEVER be the case if you're returning the ID field, since I assume this is the PRIMARY (e.g. UNIQUE) key!

Share this post


Link to post
Share on other sites

×

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.