选定的下拉框显示来自 mysql 数据库的 2 个值

selected drop box shows 2 values from mysql database

嘿,我在处理从数据库中检索的下拉菜单值时遇到了一些问题。我看到两个值而不是一个。

$con = mysqli_connect("localhost","root","") ;
$myDB = mysqli_select_db($con, "test");
$dbEnc = mysqli_set_charset($con, 'utf8');

$sqlSELECT = mysqli_query($con, 'SELECT class FROM disastergroups'); 

<!DOCTYPE html>
<html>
    <head>
        <title>test</title>
        <meta http-equiv="content-type" content = "text/html; charset=utf-8"/>
    </head>
    <body>
        <select name="test">
            <option value="">Select...</option>
            <?php while ($row1 = mysqli_fetch_array($sqlSELECT)): ;?>
            <option><?php echo implode("\t", $row1); ?></option>
            <?php endwhile;?>
        </select>
        <input type="submit" value="Submit Data">
    </body>
</html>

下图显示了下拉菜单中的重复项...我该如何解决这个问题?

这是因为 resulttypefor mysqli_fetch_array() 的默认值是 int $resulttype = MYSQLI_BOTH -> mixed mysqli_fetch_array ( mysqli_result $result [, int $resulttype = MYSQLI_BOTH ] )。变化-

while ($row1 = mysqli_fetch_array($sqlSELECT)):

while ($row1 = mysqli_fetch_array($sqlSELECT, MYSQLI_ASSOC)):

或者,只需使用 mysqli_fetch_assoc()

while ($row1 = mysqli_fetch_assoc($sqlSELECT)):

这样你只得到1个值,你可以去掉implode() ->

<option><?php echo $row1['class']; ?></option>