How can the DISTINCT keyword be used effectively in PHP queries to avoid duplicate results?

When querying a database in PHP, the DISTINCT keyword can be used to eliminate duplicate results from the query. This can be particularly useful when querying a table that may contain duplicate entries. By using DISTINCT, only unique results will be returned, avoiding duplicates in the output.

// Example code snippet using DISTINCT keyword in a SQL query
$query = "SELECT DISTINCT column_name FROM table_name";
$result = mysqli_query($connection, $query);

if(mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        echo $row['column_name'] . "<br>";
    }
} else {
    echo "No results found.";
}