What is the difference between using SELECT DISTINCT and array_unique() to eliminate duplicate entries in a MySQL query result?
When retrieving data from a MySQL database, you may encounter duplicate entries in the result set. To eliminate these duplicates, you can use the SQL query `SELECT DISTINCT` to retrieve only unique rows. Another approach is to fetch all rows and then use the PHP function `array_unique()` to remove duplicate entries from the resulting array.
// Using SELECT DISTINCT in SQL query
$sql = "SELECT DISTINCT column_name FROM table_name";
$result = mysqli_query($connection, $sql);
// Using array_unique() in PHP
$rows = [];
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row['column_name'];
}
$unique_rows = array_unique($rows);