13

我是 mysqli 的新手,并且在使用 mysqli 循环遍历结果时遇到问题。不幸的是,我只得到一个结果。当我将查询放入 phpMyAdmin 时,它会产生三个结果。我相信相关代码在这里,我只是说错了:

$connection = new mysqli($host, $databaseUsername, $databasePassword, $database);

if ($connection->connect_errno > 0) {
    die ('Unable to connect to database [' . $connection->connect_error . ']');
}

$sql = "SELECT clientId, studentFirstName, studentLastName
        FROM clients
        WHERE (studentEmail = '$postEmail') OR (parentEmail = '$postEmail');";  

if (!$result = $connection->query($sql)) {
    die ('There was an error running query[' . $connection->error . ']');
}

echo '<select class = "toolbarDropdown" id = "toolbarDropdown-MultipleAccounts">';

    while ($row = $result->fetch_array()) {
        echo '<option value="'.$row["clientId"].'">'.$row["studentFirstName"].' '.$row["studentLastName"].'</option>';
    }

echo '</select>';
4

1 回答 1

17

您在 HTML 中缺少关闭的 " at option="value <--

注意

$row = $result->fetch_array()

可以替换为

$row = $result->fetch_assoc()

这样做,您获取的每条记录的数组将占用一半大小。

于 2012-12-18T22:25:46.233 回答