![](/img/trans.png)
[英]Why I do not get first row if am using mysqli_fetch_assoc on first and mysqli_fetch_array second time?
[英]mysqli_fetch_array how to get one row at a time?
My php code is as follows: This is part of a quiz where I am displaying one question and 4 multiple choices in html page via ajax jQuery. 我知道如何運行 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_question 中的問題的 id 並通過 WHERE 子句(如 test_id)選擇一個。
//...
$test_id=$_POST["test_id"];
$questionId = filter_var($_POST['question_id'],FILTER_VALIDATE_INT);
if (!$questionId){
die('done');
}
$stmt= mysqli_prepare($con,"select * from mst_question where test_id='$test_id' AND id=?");
mysqli_stmt_bind_param(**$stmt**, 'd',$questionId);
mysqli_stmt_execute(**$stmt**);
// work with $stmt.
// f.e. your loop but now there will be only one execution
mysqli_stmt_close($stmt);
//...
$createTable .= '<input type="hidden" name="nextQuestionId" value="'.$nextQuestionId.'"/>';
//...
使用輸入字段,您將返回下一個問題的 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).'"/>';
//...
我想建議切換到 PDO - 與數據庫交互的非常友好和強大的方式。 看一個例子。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.