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>";
我正在尝试使用上面的代码为我的表单创建一个下拉列表。但它不起作用。 Branch 列中有 3 个不同的值,但在下拉列表中,它仅显示一个值(第一个),接下来的两个值显示为空白值。
然而,当在 echo $row_num 中时,它显示 3。
这意味着它获取了三行,但为什么它没有显示在下拉列表中。
如果我在 phpmyadmin 中运行相同的查询,它会显示正确的答案,它会返回 3 个不同的分支值。
$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>";
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.
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.
If I run the same query in phpmyadmin it shows the correct answer i.r it returns 3 distinct Branch values.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你应该做这样的事情:
You should do something like this:
您需要为每一行使用
mysql_fetch_array()
。 该函数返回一个的关联数组 行。只需将其包含在 echo 语句上方的 for 循环中即可。编辑: mysql_fetch_array() 实际上返回一个具有关联索引和编号索引的数组(默认情况下)。不过,您可以继续以同样的方式使用它。
you need to
mysql_fetch_array()
for each row. That function returns an associative array for one row only. just include it inside your for loop just above your echo statement.edit:
mysql_fetch_array()
actually returns an array (by default) that has associative indices and numbered indices. You can continue using it the same way, though.您需要使用以下命令循环查询:
You need to loop through your query using the following:
mysql_fetch_array 仅以数组形式返回当前数据集,并且向前移动内部指针。您需要重复调用
mysql_fetch_array
才能获取所有结果。mysql_fetch_array only returns the current dataset as an array, and moves the internal pointer ahead. You need to repeatedly call
mysql_fetch_array
to get all results.使用 while 循环的循环出现问题:
试试这个
There is a problem in the loop using a while loop:
Try this
您真正需要的是学习如何使用模板。
但看来Stackoverflow绝对不是一个可以学习专业网站开发方法的地方。
首先获取您的数据
然后在模板中使用它
What you really need is to learn how to use templates.
But it seems Stackoverflow is definitely not the place where one can learn professional ways of website developing.
get your data first
And then use it in the template
mysql_fetch_array
只会返回第一行...参见 此处了解完整详细信息:)
mysql_fetch_array
will only return the first row...see here for full details :)