限制PHP代码中数据库的数据

时间:2018-06-08 05:05:27

标签: php html mysql sql

我创建了一个测验网站。我已经创建了一个像下面这样的文件,下面的文件用于获取从中传入的数据,使用主数据库中的ajax来调用数据库中的数据



<?php
// Start the session
session_start();

$con=mysqli_connect("localhost","root","","quiz"); // change here to your data
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

// Check the number of all questions, if next_id is more than last question, back to first or whatever you want;
$response=mysqli_query($con,"select * from prometric");
$number_of_all_questions = mysqli_num_rows($response);

if($_POST['next_id'] == 0){
	// reset to default
	$_SESSION["correct_score"] = 0;
	$_SESSION["not_correct_score"] = 0;
}


if($number_of_all_questions <= $_POST['next_id']){
	// Quiz finished, show results
    echo"<div>
	<h2>Results:</h2>
	<p>Correct answers: {$_SESSION['correct_score']}</p>
	<p>Wrong answers: {$_SESSION['not_correct_score']}</p>

	</div>";



}else{

	// query next question
	$response=mysqli_query($con,"select * from prometric WHERE id =(select min(id) from prometric where id > {$_POST['next_id']})");
	?>

	<?php while($result=mysqli_fetch_array($response,MYSQLI_ASSOC)){ ?>

		<div id="question_<?= $result['id'] ?>" class='question' data-next-question="<?= $_POST['next_id'] ?>"> <!--check the class for plurals if error occurs-->
			<h2><?= $result['id'].".".$result['question_name'] ?></h2>
			<div class='align'>
				<input type="radio" value="1" id='radio1' name='1'>
				<label id='ans1' for='radio1'><?= $result['answer1'] ?></label>
				<br/>
				<input type="radio" value="2" id='radio2' name='2'>
				<label id='ans2' for='radio2'><?= $result['answer2'] ?></label>
				<br/>
				<input type="radio" value="3" id='radio3' name='3'>
				<label id='ans3' for='radio3'><?= $result['answer3'] ?></label>
				<br/>
				<input type="radio" value="4" id='radio4' name='4'>
				<label id='ans4' for='radio4'><?= $result['answer4'] ?></label>
			</div>
			<br/>
			<?php /*<input type="button" data-next-question="<?= $_POST['next_id'] ?>" id='next' value='Next!' name='question' class='butt'/> */?>
		</div>
	<?php }?>
<?php }?>
<?php mysqli_close($con); ?>
&#13;
&#13;
&#13;

每次获取数据时,数据都以相同的顺序排列,我希望数据是随机的,并将数据限制为特定数量。我怎么能这样做

2 个答案:

答案 0 :(得分:1)

只需使用 ORDER BY LIMIT

 $response=mysqli_query($con,"select * from prometric ORDER BY RAND() LIMIT 20");

答案 1 :(得分:0)

您可以使用myData %>% mutate(reflectanceSfp = case_when(bandNumber %in% badBands ~ NA_real_, TRUE ~ reflectanceSfp)) # A tibble: 6 x 5 # reflectanceSfp wavelength bandNumber reflectanceDT wavelength1 # <dbl> <dbl> <dbl> <dbl> <dbl> #1 NA 376. 1 0.000148 377. #2 NA 381. 2 0.00589 382. #3 0.0158 386. 3 0.0101 387. #4 0.0200 391. 4 0.0110 392. #5 0.0240 396. 5 0.0117 397. #6 NA 401. 6 0.0149 402. 函数随机化阵列。

如果您正在寻找限制,请使用replace