What is the purpose of using DISTINCT in a MySQL query when fetching data for PHP usage?
When fetching data for PHP usage from a MySQL database, using the DISTINCT keyword in a query helps to eliminate duplicate rows from the result set. This can be useful when you only want to retrieve unique values from a specific column or combination of columns.
<?php
// Connect to MySQL database
$connection = new mysqli("localhost", "username", "password", "database");
// Query to fetch unique values from a specific column
$query = "SELECT DISTINCT column_name FROM table_name";
// Execute the query
$result = $connection->query($query);
// Fetch and display the results
while ($row = $result->fetch_assoc()) {
echo $row['column_name'] . "<br>";
}
// Close the connection
$connection->close();
?>