hi, i m working on quiz portal in which different set of question were ask
there are 4 mutiple choice question in which one of the right answer.

i successfuly display the question by selecting using perticular test id(topic)

here is the query:*$rs=mysql_query("SELECT DISTINCT FROM mst_question WHERE test_id ='$tid' ORDER BY RAND()",$cn) or die(mysql_error());**

here also i display the question randomly but the problem is each time when i hit next question button the question repeted.
how i can prevent the reptition of question randomly i want to display one question at once with no repetation.
please if anybody have a solution please comment.
Thankyou any help is appreciated.

Dani AI

Generated

A common cause of the repeating-question symptom is re-creating the random order on every page load. Decide which uniqueness you want first (as asked): per-session, per-user, or global. For most quiz flows you want a single randomized sequence for the current attempt. That matches ’s “list and remove” idea and also addresses ’s security points: create the sequence once, keep it server-side, and use prepared statements (PDO) rather than regenerating queries on every click.

The simplest, reliable pattern is:

  • At test start fetch the list of question IDs for that test (only the IDs), shuffle them once in PHP, and store the ordered ID list plus a position pointer in the session (or in a per-attempt DB record for persistent attempts).
  • On each Next, read the current ID from the stored order, increment the pointer, and fetch that single question by ID with a prepared statement.
  • Do not re-run randomization unless the user starts a new attempt.

Example (illustrative) PHP flow using PDO:

session_start(); // required

// $pdo = your PDO instance; $testId validated and set when the test begins
if (!isset($_SESSION['quiz'][$testId])) {
    $stmt = $pdo->prepare('SELECT id FROM mst_question WHERE test_id = ?');
    $stmt->execute([$testId]);
    $ids = $stmt->fetchAll(PDO::FETCH_COLUMN);
    shuffle($ids);
    $_SESSION['quiz'][$testId] = ['order' => $ids, 'pos' => 0];
}

$state = &$_SESSION['quiz'][$testId];
if ($state['pos'] >= count($state['order'])) {
    // quiz finished
} else {
    $qid = $state['order'][$state['pos']++];
    $stmt = $pdo->prepare('SELECT * FROM mst_question WHERE id = ?');
    $stmt->execute([$qid]);
    $question = $stmt->fetch(PDO::FETCH_ASSOC);
}

Troubleshooting notes: if repetition still occurs, check that session_start() is present and sessions persist (cookies not blocked), and that you only initialize the sequence when it’s missing. To support multiple tabs/parallel attempts, create a unique attempt token and store the order under that token instead of a single global slot. For very large pools avoid ORDER BY RAND()—fetch IDs and shuffle in PHP or use sampling strategies. Log errors server-side; never expose DB errors to users.

Recommended Answers

All 3 Replies

You want to avoid:

  1. overall repetition (i.e. if I connect I don't get your same questions) or
  2. only for the current user (i.e. I never see the same questions anymore, but another user can repeat the path) or
  3. only for the current user's session (i.e. if I logout and come back I can get the same questions again)?

Make a list of all possible questions (list of question numbers or list of addresses). Pick a random entry from the list then delete that entry.

You are using obsolete and dangerous code that has been completetly removed from Php. You need to use PDO with prepared statements. PDO Tutorial

Never ever put variables in a query.
Do not output internal system messages the user. That info is only good to hackers.

Having the test id in a GET variable is a very flawed approach and can be changed by the user at will. At some point a session variable for the test should be set among other things.

It would appear your DB design is flawed as well. There is not enough info here to say much more.

This is an XY Problem which basically means "Help me with my attempted solution" rather than the real problem which is likely "How do I design a quiz application".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.