How can the DISTINCT keyword in a MySQL query be used to filter out duplicate entries and display only unique values in PHP?
When querying a MySQL database in PHP, the DISTINCT keyword can be used to filter out duplicate entries and display only unique values. This can be useful when you want to retrieve a list of unique values from a column without any duplicates. By using DISTINCT in your SQL query, you can ensure that only distinct values are returned in the result set.
<?php
// Connect to MySQL database
$connection = mysqli_connect("localhost", "username", "password", "database");
// Query to select distinct values from a column
$query = "SELECT DISTINCT column_name FROM table_name";
// Execute the query
$result = mysqli_query($connection, $query);
// Loop through the result set and display unique values
while ($row = mysqli_fetch_assoc($result)) {
echo $row['column_name'] . "<br>";
}
// Close the connection
mysqli_close($connection);
?>