【问题标题】:PHP sorting out results from a mysqli_fetch_row arrayPHP 从 mysqli_fetch_row 数组中排序结果
【发布时间】:2014-03-14 19:17:48
【问题描述】:

我有一个简单的查询来从表中提取所有数据,但我只需要显示一次该值,无论它在表中出现多少次。现在我得到以下结果:

Material in Que
9231500
9231500
9231500
Grp1298
Grp1298
6251752
6251752 

我想要的结果是:

Material in Que
9231500
6251752
Grp1298

如何从查询结果中筛选出重复的数字或文本? 对于我的表中第 3 列的数组 $row[2]

<?php session_start(); ?>  
<html>
    <head>
        <basefont face="Arial">
        <title>Material in Testing que</title>
    </head>

    <body>
    <?php

    // set database server access variables:
    include('db.php');

    // open connection
    $connection = mysqli_connect($host, $user, $pass, $db); 
    if (mysqli_connect_errno($connection)) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    // create query
    $query = "SELECT * FROM testingqa1160";
    $result = mysqli_query($connection, $query);

    if (mysqli_num_rows($result) > 0) {
        echo "Material in Testing Que";
        echo "<br>";

        while($row = mysqli_fetch_row($result)) {
            echo $row[2];
            echo "<br>";
            echo "  ";
        }
    }
    else {
        // print status message
        echo "<center>";
        echo " Que Empty </font>";
        echo "</center>";
    }

    mysqli_free_result($result);

    // close connection
    mysqli_close($connection);

    ?>
    </body>
</html>

【问题讨论】:

    标签: php mysql sql arrays distinct


    【解决方案1】:

    在查询中使用 MySQL 的 DISTINCT

    $query = "SELECT DISTINCT colname FROM testingqa1160";
    

    【讨论】:

      【解决方案2】:

      你可以给我们group by子句

      $query = "SELECT * FROM testingqa1160 GROUP BY colname";
                      $result = mysqli_query($connection, $query);
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-20
      • 2014-12-04
      • 2012-07-11
      相关资源
      最近更新 更多