简体   繁体   English

是否有任何查询允许用户一次仅从数据库中选择随机行

[英]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 在我正在做的PHP文件中

$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. 将查询结果存储在Web应用程序的会话中。 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. 如果不是,则查询数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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