我有一个数据库,我正在查询它以获取所有行。然后,我得到一个名为id
的变量标识的特定记录,该变量来自somepage.php?id=aninteger
出于某种原因,我无法弄清楚如何在数据库中获得数字id
的结果。 (从0开始)。
这是我的代码:
<?php
$connection = mysqli_connect("host","user","password","database");
$id = $_POST['id'];
$result = mysqli_query($connection, "SELECT * FROM Scavenger");
$resultIDArray = array();
while ($row = mysqli_fetch_array($result)) {
array_push($resultIDArray, $row['ID']);
}
$resultQArray = array();
while ($row = mysqli_fetch_array($result)) {
array_push($resultQArray, $row['Key']);
}
$resultAArray = array();
while ($row = mysqli_fetch_array($result)) {
array_push($resultAArray, $row['Answer']);
}
echo "<p>BlahBlah<br>BlahBlah<br><br><br>".$resultQArray[(int) $id]."<br><br></p>";
echo "<form action='checkAnswer.php' method='post'><input type='text' name='Answer' id='Answer'><input type='submit' value='Submit'></form>";
?>
我知道$resultQArray[(int) $id]
有问题,但对于我的生活,我似乎无法弄清楚是什么。
答案 0 :(得分:0)
你正在第一次循环中走遍所有查询结果,所以所有后续循环都在第一次通过条件检查失败....尝试第一次完成所有工作。
$connection = mysqli_connect("host","user","password","database");
$id = $_POST['id'];
$result = mysqli_query($connection, "SELECT * FROM Scavenger");
$resultIDArray = array()
$resultQArray = array();
$resultAArray = array();
while ($row = mysqli_fetch_array($result)) {
array_push($resultIDArray, $row['ID']);
array_push($resultQArray, $row['Key']);
array_push($resultAArray, $row['Answer']);
}
echo "<p>BlahBlah<br>BlahBlah<br><br><br>".$resultQArray[(int) $id]."<br><br></p>";
echo "<form action='checkAnswer.php' method='post'><input type='text' name='Answer' id='Answer'><input type='submit' value='Submit'></form>";
&GT;