What is the purpose of using DISTINCT in a MySQL query when retrieving data for a PHP application?

Using DISTINCT in a MySQL query helps to eliminate duplicate rows from the result set, ensuring that only unique values are returned. This can be useful when retrieving data for a PHP application to prevent displaying redundant information to users. By using DISTINCT, you can streamline the data being retrieved and improve the efficiency of your application.

// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Query the database with DISTINCT
$sql = "SELECT DISTINCT column_name FROM table_name";
$result = $conn->query($sql);

// Output the results
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "Column Value: " . $row["column_name"] . "<br>";
    }
} else {
    echo "0 results";
}

// Close the connection
$conn->close();