0

我有一个数据库,我正在查询它以获取所有行。然后,我得到一个特定的记录,该记录由一个名为的变量标识,该变量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],但对于我的生活,我似乎无法弄清楚是什么。

4

1 回答 1

0

您在第一个 where 循环中遍历所有查询结果,因此所有后续循环第一次都未通过条件检查....尝试第一次完成所有工作。

    $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>";

?>

于 2013-08-21T22:15:44.690 回答