masgas Posted February 18, 2006 Share Posted February 18, 2006 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! Link to comment https://forums.phpfreaks.com/topic/3469-stop-repeating-question/ Share on other sites More sharing options...
fenway Posted February 18, 2006 Share Posted February 18, 2006 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. Link to comment https://forums.phpfreaks.com/topic/3469-stop-repeating-question/#findComment-11925 Share on other sites More sharing options...
masgas Posted February 19, 2006 Author Share Posted February 19, 2006 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... Link to comment https://forums.phpfreaks.com/topic/3469-stop-repeating-question/#findComment-11942 Share on other sites More sharing options...
fenway Posted February 19, 2006 Share Posted February 19, 2006 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! Link to comment https://forums.phpfreaks.com/topic/3469-stop-repeating-question/#findComment-11974 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.