PHP从mysqli_fetch_row数组中挑选出结果

时间:2014-03-14 19:17:48

标签: php mysql sql arrays distinct

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

Material in Que
9231500
9231500
9231500
Grp1298
Grp1298
6251752
6251752 

我想要的结果是什么:

Material in Que
9231500
6251752
Grp1298

如何从查询结果中整理出重复的数字或文字? 对于我的表$ row [2]

中包含第3列的数组
<?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>

2 个答案:

答案 0 :(得分:3)

在查询中使用MySQL的DISTINCT

$query = "SELECT DISTINCT colname FROM testingqa1160";

答案 1 :(得分:0)

你可以group by条款

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