mysql在PHP中选择不同的查询 [英] mysql select distinct query in PHP
本文介绍了mysql在PHP中选择不同的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$sql = "SELECT DISTINCT Branch FROM student_main";
$result = mysql_query($sql);
$row_num = mysql_num_rows($result);
$rows = mysql_fetch_array($result);
echo "<select name='Branch'>";
for($i=0;$i<=$row_num-1;$i++){
echo "<option value='".$rows[$i]."'>".$rows[$i]."</option>";
}
echo "</select>";
echo "<input type='submit' Value='submit' />";
echo "</form>";
我正在尝试使用上述代码为我的表单创建一个下拉列表。但它不工作。 分支列中有3个不同的值,但在下拉列表中,它只显示一个值(第一个),而后两个值作为空值。
I am trying to create a dropdown using the above code for my form. But its not working. There are 3 distinct values in the Branch column but in the dropdown, it shows only one value(the first one) and the next two as blank values.
但是在echo $ row_num中,它显示为3.
这意味着它提取三行,但为什么它不显示在下拉列表中名单。
However when in echo $row_num, its shows 3.
Thats means its fetching the three rows, but then why its not showing in the dropdown list.
如果我在phpmyadmin中运行相同的查询,它会显示正确的答案,它会返回3个不同的分支值。
If I run the same query in phpmyadmin it shows the correct answer i.r it returns 3 distinct Branch values.
推荐答案
你应该这样做:
$sql = "SELECT DISTINCT Branch FROM student_main";
$result = mysql_query($sql);
echo "<select name='Branch'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='".$row[0]."'>".$row[0]."</option>";
}
echo "</select>";
echo "<input type='submit' Value='submit' />";
echo "</form>";
这篇关于mysql在PHP中选择不同的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文