循环通过mysqli的结果

时间:2012-12-18 22:06:04

标签: php mysqli while-loop

我是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>';

1 个答案:

答案 0 :(得分:15)

您在HTML

中缺少结束“at option =”值&lt; -

请注意

$row = $result->fetch_array()

可以替换为

$row = $result->fetch_assoc()

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