0

i have a table named quiz ,it have six columns like id,question,option1,option2,option3,answer.i want to loop through all the values by using the following query

  $sql = "SELECT id, option1, option2,option3,answer,question FROM quiz";
  $result = $conn->query($sql);
   if ($result->num_rows > 0) {
   // output data of each row
     while($row = $result->fetch_assoc()) {
    echo "id: " . $row["id"]. " - question: " . $row["question"]. " " . 
  $row["option1"]. "<br>";
   }

How can i get random values from mysql table everytime i want only 4 values.I am new to development .

Akhil B
  • 69
  • 3
  • 10

1 Answers1

1

welcome to Stackoverflow! Use this;

$sql = "SELECT id, option1, option2,option3,answer,question FROM quiz ORDER BY RAND() LIMIT 4";

This will get rows 100% randomly! But, it can get you duplicates..

Aaron Jonk
  • 473
  • 2
  • 7
  • 21