mysql不同以形成选择

时间:2017-03-04 00:58:41

标签: php mysql

我必须创建一个从mySQL数据库中获取选项的表单,以便列出一列中的所有不同值。无效的代码如下:

<form class="form-horizontal" method="get" action="startlist.php">
<select id="selectbasic" name="klass" class="form-control">
<?PHP
$connection = mysqli_connect("link","dbuser","pass","dbname");
mysqli_set_charset($connection,"utf8");
$sql = "SELECT DISTINCT Klass FROM Voistlejad";
$result = mysqli_query($connection,$sql);
while ($row = mysqli_fetch_array($connection,$result)) {
echo $row["Klass"];
echo "<option value='".$row[0]."'>".$row[0]."</option>";
}
mysqli_close($connection);
?>
    <option value='32KK5B'>32KK5B</option><!--This is how it needs to be-->
</select>
<button type='submit' class="btn btn-primary">Move on</button>
</form> 

有什么问题?

1 个答案:

答案 0 :(得分:0)

我认为你在混淆使用单引号和双引号的地方.. 通常,对于像$ row这样的数组/对象,您可以从数组中获取没有引号或单引号的值,echo $ row [&#39; Klass&#39;]

while ($row = mysqli_fetch_array($connection,$result)) {

echo $row['Klass']; // single quotes

echo "<option value='". $row['Klass'] ."'>". $row['Klass'] ."</option>";
 } 
// shows sql error ONLY if there is one to show
 printf(" %s\n", mysqli_error($link));

P.S强烈建议添加代码来显示MySQLi错误,因为事情发生了!