php 在mysqli中循环结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13942268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:18:43  来源:igfitidea点击:

Looping through results in mysqli

phpmysqliwhile-loop

提问by radleybobins

I am new to mysqli and having a problem looping through results with mysqli. Unfortunately, I am only getting a single result. When I put the query into phpMyAdmin, it comes up with three results. I believe the relevant code is here and that I am just calling it wrong:

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

回答by hek2mgl

You are missing the closing " at option="value <-- in your HTML

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

Note that

注意

$row = $result->fetch_array()

can be replaced by

可以替换为

$row = $result->fetch_assoc()

Doing so, the array for each record you fetch would take half of the size.

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