mysql 在 PHP 中选择不同的查询

发布于 2024-10-16 23:47:03 字数 692 浏览 2 评论 0原文

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

土豪 2024-10-23 23:47:04

你应该做这样的事情:

$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>";

You should do something like this:

$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>";
记忆消瘦 2024-10-23 23:47:04

您需要为每一行使用 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.

迷路的信 2024-10-23 23:47:04

您需要使用以下命令循环查询:

    $sql = "SELECT DISTINCT Branch FROM student_main";
    $result = mysql_query($sql);
    echo "<select name='Branch'>";
    while($rows = mysql_fetch_array($result)){ // should probably use mysql_fetch_assoc()
        echo "<option value='".$rows['Branch']."'>".$rows['Branch']."</option>";
    }
    echo "</select>";
    echo "<input type='submit' Value='submit' />";
    echo "</form>";

You need to loop through your query using the following:

    $sql = "SELECT DISTINCT Branch FROM student_main";
    $result = mysql_query($sql);
    echo "<select name='Branch'>";
    while($rows = mysql_fetch_array($result)){ // should probably use mysql_fetch_assoc()
        echo "<option value='".$rows['Branch']."'>".$rows['Branch']."</option>";
    }
    echo "</select>";
    echo "<input type='submit' Value='submit' />";
    echo "</form>";
旧伤慢歌 2024-10-23 23:47:04

mysql_fetch_array 仅以数组形式返回当前数据集,并且向前移动内部指针。您需要重复调​​用mysql_fetch_array才能获取所有结果。

while ($row = mysql_fetch_array($result)) {
    echo "<option value='".$row['Branch']."'>".$row['Branch']."</option>";
}

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 ($row = mysql_fetch_array($result)) {
    echo "<option value='".$row['Branch']."'>".$row['Branch']."</option>";
}
那小子欠揍 2024-10-23 23:47:04

使用 while 循环的循环出现问题:

while($rows=mysql_fetch_array($result)){ 

echo "<option value='".$rows[$i]."'>".$rows[$i]."</option>";

}

试试这个

There is a problem in the loop using a while loop:

while($rows=mysql_fetch_array($result)){ 

echo "<option value='".$rows[$i]."'>".$rows[$i]."</option>";

}

Try this

所谓喜欢 2024-10-23 23:47:04

您真正需要的是学习如何使用模板。
但看来Stackoverflow绝对不是一个可以学习专业网站开发方法的地方。

首先获取您的数据

$select = $array();
$sql = "SELECT DISTINCT Branch FROM student_main";
$res = mysql_query($sql) or trigger_error(mysql_error().$sql);
while($row = mysql_fetch_array($res)) $select = $row[];

然后在模板中使用它

<form>
<select name='Branch'>
<? foreach($select as $row): ?>    
  <option value="<?=htmlspecialchars($row['Branch'])?>">
    <?=htmlspecialchars($row['Branch'])?>
  </option>
<? endforeach ?>    
</select>
<input type='submit' Value='submit' />
</form>

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

$select = $array();
$sql = "SELECT DISTINCT Branch FROM student_main";
$res = mysql_query($sql) or trigger_error(mysql_error().$sql);
while($row = mysql_fetch_array($res)) $select = $row[];

And then use it in the template

<form>
<select name='Branch'>
<? foreach($select as $row): ?>    
  <option value="<?=htmlspecialchars($row['Branch'])?>">
    <?=htmlspecialchars($row['Branch'])?>
  </option>
<? endforeach ?>    
</select>
<input type='submit' Value='submit' />
</form>
真心难拥有 2024-10-23 23:47:04

mysql_fetch_array 只会返回第一行...

参见 此处了解完整详细信息:)

mysql_fetch_array will only return the first row...

see here for full details :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文