我最近决定使用较新版本的PHP测试我真正的旧项目,因此我必须从mysqli_fetch_assoc()
切换到<?php
$query = 'SELECT people_id, people_fullname FROM people WHERE people_isactor = 1 ORDER BY people_fullname;';
$result = mysqli_query($con,$query) or die(mysqli_error($con));
//populate the select options with the result
while($row = mysqli_fetch_assoc($result)){
foreach($row as $value){
if($row['people_id'] == $movie_leadactor){
echo '<option value="'.$row['people_id'].'"selected="selected">';
}else{
echo '<option value="'.$row['people_id'].'">';
}
echo $row['people_fullname'].'<option>';
}
}
?>
。
但是,在我的localhost上运行它时,数据将被返回两次。
以下是代码段。任何帮助将不胜感激。
提前致谢:)
Link
答案 0 :(得分:2)
1.删除foreach()
2.Last <option>
必须为</option>
。
如下所示: -
<?php
$query = 'SELECT people_id, people_fullname FROM people WHERE people_isactor = 1 ORDER BY people_fullname;';
$result = mysqli_query($con,$query) or die(mysqli_error($con));
//populate the select options with the result
while($row = mysqli_fetch_assoc($result)){
if($row['people_id'] == $movie_leadactor){
echo '<option value="'.$row['people_id'].'"selected="selected">'.$row['people_fullname'].'</option>';
}else{
echo '<option value="'.$row['people_id'].'">'.$row['people_fullname'].'</option>';
}
}
?>