简体   繁体   中英

is there any query that allows user to select random rows from database one time only

I want query to select random rows from database only for one time.I mean to say after one time if user press refresh button rows should remains the same. currently i am doing

SELECT * from table-name order by rand() limit 5;

this query selects random rows every time when user press refresh button or refresh page.

in php file i am doing

$query="SELECT * from question_bank order by rand() limit 10";
$result= mysqli_query($connection,$query);
confirm_query($result);

?>

<form action="result.php" name="form1" method="post" id="quiz">
<?php
while ($read_all_data = mysqli_fetch_assoc($result))
{ 
    $_SESSION['id']=1;

$id=$read_all_data['question_id'];
$a=$read_all_data['a'];
$b=$read_all_data['b'];
$c=$read_all_data['c'];
$d=$read_all_data['d'];
 echo $read_all_data['question']."</br>";
 echo "A:<input type ='radio' value ='a'  name='$id' >".$read_all_data['a']."</br>";
 echo "B:<input type ='radio' value ='b'  name='$id' >".$read_all_data['b']."</br>";
 echo "C:<input type ='radio' value ='c'  name='$id' >".$read_all_data['c']."</br>";
 echo "D:<input type ='radio' value ='d'  name='$id' >".$read_all_data['d']."</br>";

}

Store the results of the query in the session of your web application. During page load check if the results are in the session and display them from there if they are. If not, then query the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM