douchaqi3369 2018-06-08 05:05
浏览 13

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

i have created a quiz website. i have created a file like below and t the below file is used to fetch the data comming from it is called using ajax in the main html page.data from database

<?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); ?>

the data comes in the same order every time i fetch it, i want the data to be random and limit the data to certain number. how can i do this

</div>
  • 写回答

2条回答 默认 最新

  • dongwei6700 2018-06-08 05:08
    关注

    Just use ORDER BY and LIMIT

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

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答