1

I have a simple query to pull all the data from a table but I only need display the value once no matter how many times its in the table. Right now I get the following results:

Material in Que
9231500
9231500
9231500
Grp1298
Grp1298
6251752
6251752 

What my desired result is to get:

Material in Que
9231500
6251752
Grp1298

How Do I sort out repeating numbers or Text from my query results? For the array with column 3 from my table $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>

2 Answers 2

3

Use MySQL's DISTINCT in your query:

$query = "SELECT DISTINCT colname FROM testingqa1160";
Sign up to request clarification or add additional context in comments.

Comments

0

you can us group by clause

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

1 Comment

Group by is not an efficient way to retrieve non duplicate items. Using distinct to get unique values is the correct way. Group by is for aggregate functions - check this dev.mysql.com/doc/refman/5.7/en/group-by-functions.html.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.