下拉选项查询sql返回重复

时间:2015-11-06 05:18:42

标签: php drop-down-menu

如何停止查询我的下拉列表重复,如下图所示?

Dropdown image

请选择CD价格:

<select name="CDPrice">
    <option value="">
        <?php
        include 'database_conn.php';
        if (!( is_object($conn) && ( get_class($conn) == 'mysqli' ))) {
            die("DB connection failure.");
        }
        $rsCDprice = mysqli_query($conn, "SELECT nmc_cd.CDPrice FROM nmc_cd");
        if (!$rsCDprice) {
            die("No result from DB query."); //probably invalid SQL, table error, etc.
        }
        if ($rsCDprice->num_rows < 1) {
            die("No rows returned from DB query."); //query runs OK, but nothing is found in DB to match.
        }
        while ($Catpriceresult = mysqli_fetch_array($rsCDprice)) {
            echo "<option value='" . $Catpriceresult[0] . "'>" . $Catpriceresult[0] . "</option>";
        }
        ?>
</select>

5 个答案:

答案 0 :(得分:1)

您可以使用DISTINCTgroup by

  1. group by

    SELECT nmc_cd.CDPrice FROM nmc_cd GROUP BY nmc_cd.CDPrice

  2. DISTINCT

    SELECT DISTINCT nmc_cd.CDPrice FROM nmc_cd

答案 1 :(得分:1)

从数据库中选择DISTINCT值(唯一值)

double double_values[18];
for(int i = 0; i < 18; i++) {
    //Your code here
}  

答案 2 :(得分:1)

这应该这样做:

SELECT DISTINCT nmc_cd.CDPrice FROM nmc_cd
祝你好运!!

答案 3 :(得分:1)

table中,column可能包含许多duplicate values;有时您只想列出different (distinct) values

DISTINCT 关键字可用于仅返回不同(不同)的值。

语法:

 SELECT DISTINCT column_name,column_name
    FROM table_name;

答案 4 :(得分:1)

尝试执行以下代码。 请选择CD价格:

<select name="CDPrice">
    <option value="">
        <?php
        include 'database_conn.php';
        if (!( is_object($conn) && ( get_class($conn) == 'mysqli' ))) {
            die("DB connection failure.");
        }
        $rsCDprice = mysqli_query($conn, "SELECT DISTINCT nmc_cd.CDPrice FROM nmc_cd");
        if (!$rsCDprice) {
            die("No result from DB query."); //probably invalid SQL, table error, etc.
        }
        if ($rsCDprice->num_rows < 1) {
            die("No rows returned from DB query."); //query runs OK, but nothing is found in DB to match.
        }
        while ($Catpriceresult = mysqli_fetch_array($rsCDprice)) {
            echo "<option value='" . $Catpriceresult[0] . "'>" . $Catpriceresult[0] . "</option>";
        }
        ?>
</select>