我有一个名为'fishdata'的MySQL数据库,里面有一个名为'Fish'的表,如下所示:
FishType | X_B | X_SB |
|沙丁鱼| 1#2#3 | 7#4#6 |
|鳕鱼| 3#6#8 | 7#4#6 |
|鲶鱼| 4#3#8 | 7#4#6 |
我正在尝试将“FishType”列中的所有条目显示为选择框中的选项。这是我的代码:
<html>
<body>
<select>
<?php
$con=mysqli_connect("localhost","root","root","fishdata");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT * FROM Fish";
$result = mysqli_query($con,$sql)or die(mysqli_error());
while($row = mysqli_fetch_array($result)) {
{
?>
<option value = "<?php echo($row['FishType'])?>" >
<?php echo($row['FishType']) ?>
</option>
<?php
}
?>
</select>
</body>
</html>
但是,选择框显示时没有选项。有什么想法吗?
答案 0 :(得分:0)
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo "<option value='" . $row['FishType'] . "'>" . $row['FishType'] . "</option>";
}
你的while循环旁边有一个额外的括号。
修改强>
<html>
<head></head>
<body>
<?php $con = mysqli_connect("localhost","root","root","fishdata");
if (mysqli_connect_errno()){ echo "Failed to connect to MySQL: " . mysqli_connect_error(); }
$result = mysqli_query($con, "SELECT FishType FROM Fish");
if (mysqli_num_rows($result) > 0){
echo "Records found";
echo "<select>";
while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo "<option value='Test'>Test</option>";
echo "<option value=\"" . $row['FishType'] . "\">" . $row['FishType'] . "</option>";
}
echo "</select>";
}
mysqli_close($con);
?>
</body>
</html>