mysql 在 PHP 中选择不同的查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4959155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:22:02  来源:igfitidea点击:

mysql select distinct query in PHP

phpselectdrop-down-menudistinct

提问by hsinxh

$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.

我正在尝试使用上面的代码为我的表单创建一个下拉列表。但它不起作用。Branch 列中有 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.

但是,当在 echo $row_num 中时,它显示 3。
这意味着它获取了三行,但是为什么它没有显示在下拉列表中。

If I run the same query in phpmyadmin it shows the correct answer i.r it returns 3 distinct Branch values.

如果我在 phpmyadmin 中运行相同的查询,它会显示正确的答案,它返回 3 个不同的 Branch 值。

回答by Scott M.

you need to mysql_fetch_array()for each row. That functionreturns an associative array for onerow only. just include it inside your for loop just above your echo statement.

你需要mysql_fetch_array()为每一行。该函数返回一个关联数组一个只有一行。只需将它包含在您的 for 循环中,就在您的 echo 语句上方。

edit: mysql_fetch_array()actually returns an array (by default) that has associative indices andnumbered indices. You can continue using it the same way, though.

编辑:mysql_fetch_array()实际上返回一个具有关联索引编号索引的数组(默认情况下)。不过,您可以继续以相同的方式使用它。

回答by Prisoner

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

回答by aorcsik

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

回答by Reiner Gerecke

mysql_fetch_arrayonly returns the current dataset as an array, and moves the internal pointer ahead. You need to repeatedly call mysql_fetch_arrayto get all results.

mysql_fetch_array仅将当前数据集作为数组返回,并将内部指针向前移动。您需要反复调用mysql_fetch_array才能获得所有结果。

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

回答by Sudantha

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

使用while循环的循环有问题:

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

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

}

Try this

尝试这个

回答by Your Common Sense

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.

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

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>

回答by simon

mysql_fetch_arraywill only return the first row...

mysql_fetch_array只会返回第一行...

see herefor full details :)

详情请看这里:)