How does the DISTINCT keyword in SQL play a role in preventing duplicate results when working with MySQL in PHP?

When working with MySQL in PHP, the DISTINCT keyword is used to eliminate duplicate rows from the result set. This is useful when querying a database and wanting to ensure that each row returned is unique based on the specified columns. By using DISTINCT, you can prevent duplicate results from being displayed in your PHP application.

<?php
// Connect to MySQL database
$connection = mysqli_connect("localhost", "username", "password", "database");

// Query to select distinct rows based on a specific column
$query = "SELECT DISTINCT column_name FROM table_name";

$result = mysqli_query($connection, $query);

// Loop through results and display them
while ($row = mysqli_fetch_assoc($result)) {
    echo $row['column_name'] . "<br>";
}

// Close database connection
mysqli_close($connection);
?>