selected drop box shows 2 values from mysql database

47 views Asked by At

Hey I am having some trouble with my dropdown menu values that I retrieve from my database. I see two values instead of one.

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

The image below shows the duplicates in the dropdown menu... how do I fix this?

CLICK HERE FOR IMAGE

1

There are 1 answers

8
Sean On BEST ANSWER

This is because the default of resulttypefor mysqli_fetch_array() is int $resulttype = MYSQLI_BOTH -> mixed mysqli_fetch_array ( mysqli_result $result [, int $resulttype = MYSQLI_BOTH ] ). Change -

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

to

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

Or, just use mysqli_fetch_assoc()

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

so that you only get the 1 value, and you can remove the implode() ->

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