Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从数据库中获取随机数据_Php_Mysql - Fatal编程技术网

Php 从数据库中获取随机数据

Php 从数据库中获取随机数据,php,mysql,Php,Mysql,我是php新手。我正在创建小型在线考试应用程序。我在MySql中创建了一个问题表,只想在页面上显示一个问题。提交该问题的答案后,在页面上显示另一个问题。有人能帮我吗?我该怎么做?谢谢从问题中按兰德顺序选择*限制4编辑2 下面是更新伪代码以修复原始代码的一些问题 基本上,这里的变化是从数据库中获取所有问题ID,然后洗牌。这避免了自动递增序列中丢失ID的情况,这很可能是错误的 旧代码的另一个变化是从数据库中获取所有选定的问题,而不是一次一个。不知道我在想什么 以下是一些伪代码: // Get all

我是php新手。我正在创建小型在线考试应用程序。我在MySql中创建了一个问题表,只想在页面上显示一个问题。提交该问题的答案后,在页面上显示另一个问题。有人能帮我吗?我该怎么做?谢谢

从问题中按兰德顺序选择*限制4

编辑2 下面是更新伪代码以修复原始代码的一些问题

基本上,这里的变化是从数据库中获取所有问题ID,然后洗牌。这避免了自动递增序列中丢失ID的情况,这很可能是错误的

旧代码的另一个变化是从数据库中获取所有选定的问题,而不是一次一个。不知道我在想什么

以下是一些伪代码:

// Get all questions ids. This should be fine since there shouldn't be too many cases where you will have more than 1000 questions.
$questionIds = db.selectIdsFromQuestionsWhereTypeIsSports();

// Shuffle array so the question ids are out of order
shuffle($questionIds);

// Number of questions you want
$quizLength = 5;

// select your questions    
$selectedQuestions = array_slice($questionIds, 0, $quizLength);

// Now fetch all data for selected questions
$quiz = db.fetchByWhereIdIn($selectedQuestions);

// Now do whatever with your question
// Get a count of your questions from the database
$totalQuestions = db.count();

// Generate an array sequence/range
$questionIds = range(1, $totalQuestions);

// Shuffle array so the numbers are out of order
shuffle($questionIds);

// Store your questions    
$quiz = array();

// Number of questions you want
$quizLength = 5;

// Now you can retrieve questions like so
while (count($quiz) == $quizLength) {
   $question = db.fetchById(array_pop($questionIds);
   if ($question != null) {
      $quiz[] = $question; 
   }
}
// Now do whatever with your question
**原创的 我不会使用MySQL
rand
函数。如果您有很多行,它不会提供很好的性能。此外,您还有机会再次选择相同的问题

所以我要做的是从数据库中检索您的问题集,然后在php中洗牌

如果您有数千个问题,那么我建议您随机生成一个与自动增量id相关的数字序列。如果您不使用自动增量id,那么这将不起作用

所以,如果你想问10个问题,并且数据库中有100个问题,那么就生成10个介于1和100之间的数字

这种方法的一个缺点是,如果在自动递增序列中存在漏洞。如果你没有太多的数字,你可以把它扔掉,然后随机选择另一个数字

以下是一些伪代码:

// Get all questions ids. This should be fine since there shouldn't be too many cases where you will have more than 1000 questions.
$questionIds = db.selectIdsFromQuestionsWhereTypeIsSports();

// Shuffle array so the question ids are out of order
shuffle($questionIds);

// Number of questions you want
$quizLength = 5;

// select your questions    
$selectedQuestions = array_slice($questionIds, 0, $quizLength);

// Now fetch all data for selected questions
$quiz = db.fetchByWhereIdIn($selectedQuestions);

// Now do whatever with your question
// Get a count of your questions from the database
$totalQuestions = db.count();

// Generate an array sequence/range
$questionIds = range(1, $totalQuestions);

// Shuffle array so the numbers are out of order
shuffle($questionIds);

// Store your questions    
$quiz = array();

// Number of questions you want
$quizLength = 5;

// Now you can retrieve questions like so
while (count($quiz) == $quizLength) {
   $question = db.fetchById(array_pop($questionIds);
   if ($question != null) {
      $quiz[] = $question; 
   }
}
// Now do whatever with your question

将会话中的提问ID设置为逗号分隔

$_SESSION['asked_question_ids'] = $asked_question_ids;
使用会话中的问题ID获取尚未询问的随机问题

$asked_question_ids = '3, 4, 5, asked queston ids from session'

SELECT * FROM questions WHERE qid NOT IN ($asked_question_ids) ORDER BY RAND() LIMIT 1