Php mysqli_fetch_数组如何一次获取一行?

Php mysqli_fetch_数组如何一次获取一行?,php,jquery,ajax,mysqli-fetch-array,Php,Jquery,Ajax,Mysqli Fetch Array,我的php代码如下:这是测验的一部分,我将通过ajax jQuery在html页面中显示一个问题和4个选项。我知道如何运行while循环并逐个显示所有数据,但如何一次只显示一个问题 所以在回答了一个问题之后,我想看看下一个问题。是否可以运行计数器,一次提取一个结果,然后提取下一个结果,依此类推 <?php header("Access-Control-Allow-Origin: *"); require 'db.php'; // making empty variable $create

我的php代码如下:这是测验的一部分,我将通过ajax jQuery在html页面中显示一个问题和4个选项。我知道如何运行while循环并逐个显示所有数据,但如何一次只显示一个问题

所以在回答了一个问题之后,我想看看下一个问题。是否可以运行计数器,一次提取一个结果,然后提取下一个结果,依此类推

<?php 
header("Access-Control-Allow-Origin: *");
require 'db.php';
// making empty variable
$createTable = "";

        $test_id=$_POST["test_id"];
        $sql=mysqli_query($con,"select * from mst_question where test_id='$test_id' ");
    $counter = 0;

while($row=mysqli_fetch_array($sql))
        {   
    $counter++;
        $createTable .= '<div class="text-subhead-2 text-center" style="background-color:#42A5F5">Question ';
        $createTable .= $counter;
        $createTable .= ' of 25</div>';
        $createTable .= '<div class="panel panel-default paper-shadow" data-z="0.5">';

        $createTable .= '<div class="panel-body">';
        $createTable .= '<p class="text-body-2">';
        $createTable .= $row['que_desc'];
        $createTable .= '</p>';
       $createTable .= '</div>';
        $createTable .= '</div>';

        $createTable .= '<div class="text-subhead-2 text-light">Your Answer</div>';
        $createTable .= '<div class="panel panel-default paper-shadow" data-z="0.5">';
        $createTable .= '<div class="panel-body">';
        $createTable .= '<div class="radio radio-success">';
        $createTable .= '<input type="radio" name="radio';
        $createTable .= $counter;
        $createTable .= '" id="radio1';
        $createTable .= $counter;
        $createTable .= '" value="';
        $createTable .= $row['ans1'];
        $createTable .= '" >';
        $createTable .= '<label for="radio1';
        $createTable .= $counter;
        $createTable .= '">';
        $createTable .= $row['ans1'];
        $createTable .= '</label>';
        $createTable .= '</div>';
        $createTable .= '<div class="radio radio-success">';
        $createTable .= '<input type="radio" name="radio';
        $createTable .= $counter;
        $createTable .= '" id="radio2';
        $createTable .= $counter;
        $createTable .= '" value="';
        $createTable .= $row['ans2'];
        $createTable .= '" >';
        $createTable .= '<label for="radio2';
        $createTable .= $counter;
        $createTable .= '">';
        $createTable .= $row['ans2'];
        $createTable .= '</label>';
        $createTable .= '</div>';
        $createTable .= '<div class="radio radio-success">';
        $createTable .= '<input type="radio" name="radio';
        $createTable .= $counter;
        $createTable .= '" id="radio3';
        $createTable .= $counter;
        $createTable .= '" value="';
        $createTable .= $row['ans3'];
        $createTable .= '" >';
        $createTable .= '<label for="radio3';
        $createTable .= $counter;
        $createTable .= '">';
        $createTable .= $row['ans3'];
        $createTable .= '</label>';
        $createTable .= '</div>';
        $createTable .= '<div class="radio radio-success">';
        $createTable .= '<input type="radio" name="radio';
        $createTable .= $counter;
        $createTable .= '" id="radio4';
        $createTable .= $counter;
        $createTable .= '" value="';
        $createTable .= $row['ans4'];
        $createTable .= '" >';
        $createTable .= '<label for="radio4';
        $createTable .= $counter;
        $createTable .= '">';
        $createTable .= $row['ans4'];
        $createTable .= '</label>';
        $createTable .= '</div>';
        $createTable .= '</div>';
        $createTable .= '</div>';

                        }

    echo $createTable;
    mysqli_close($con);
    ?>

首先,您的代码是危险的,因为可以通过sql注入进行黑客攻击。您应该始终使用参数绑定

最简单的方法是传递存储在mst_问题中的问题id,并选择一个by WHERE子句(如test_id)

/。。。
$test\u id=$\u POST[“test\u id”];
$questionId=filter\u var($\u POST['questionId'],filter\u VALIDATE\u INT);
如果(!$questionId){
死亡(“完成”);
}
$stmt=mysqli_prepare($con,“从mst_问题中选择*,其中test_id='$test_id'和id=?”);
mysqli_stmt_bind_参数(**$stmt**,'d',$questionId);
mysqli_stmt_execute(**$stmt**);
//使用$stmt。
//f.e.您的循环,但现在只有一次执行
mysqli_stmt_close($stmt);
//...
$createTable.='';
//...
使用输入字段,您将返回下一个问题的id,该id可以在javascript代码的url参数中传递

如果您担心测验作弊者,可以通过散列nextQuestionId来提高安全性

//...
$stmt = mysqli_prepare($con,"select * from mst_question where test_id='$test_id' AND sha1(CONCAT('slat_',id))=?");
//...
$createTable .= '<input type="hidden" name="nextQuestionId" value="'.sha1('salt_'.$nextQuestionId).'"/>';
//...
/。。。
$stmt=mysqli_prepare($con,“从mst_问题中选择*,其中test_id='$test_id'和sha1(CONCAT('slat_',id))=?”;
//...
$createTable.='';
//...
这不是最好的解决方案,但需要对代码进行最小的更改。
我想建议切换到PDO——一种非常友好和强大的与数据库交互的方式

如何从数据库中获取问题id,以便将其发布到php,一次只获取一个问题???是否有一种方法可以基于来自循环的相同测试id序列化每个问题id,并以post?快速编写的方式一次发送一个。当用户第一次访问页面时,类似这样的情况:(在另一个使用ajax JS脚本呈现UI的脚本中)
php echo“echo”var question_id=“.mysqli_query($con,“从mst_question中选择min(id))->fetch_object()->id.;”;echo”“
接下来,在JS脚本中,您应该使用类似test\u id的问题id(通过ajax执行的脚本)
php$nextQuestionId=mysqli\u query($con,“从mst\u question中选择min(id),其中id>”$questionId)->fetch\u object()->id//删除主答案中带有示例的sqlinjection