Tudor Manole Tudor Manole - 6 months ago 17
PHP Question

Duplicate results from MySQL table?

I have the following code:

<?php

$select = mysqli_query ($con,"SELECT field_2 FROM info4");
if (! $select) {
die('Query unfunctional ' . mysqli_error($con));
}
?>

<table>
<td><select name="City">

<?php
while ($res=mysqli_fetch_array($select)){
foreach($res as $value){
echo "<option>" . $value . "</option>";
}}
?>
</select></td></table>


where
$con
is the connection.

But this outputs the dropdown with all of the values duplicated. For example if I had 1 and 2 in my table, it outputs: 1/1/2/2 (in the dropdown). I'm supposing I'm making a mistake in my code--can anyone see it by any chance?

Thanks in advance.

Answer

try this. foreach loop in this case is unneccessary.

<?php
while ($res=mysqli_fetch_assoc($select)){
echo "<option>" . $res["field_2"] . "</option>";
}
?>