In what scenarios would using SELECT DISTINCT in a SQL query be more effective than using array_unique() in PHP to remove duplicate entries?

Using SELECT DISTINCT in a SQL query would be more effective than using array_unique() in PHP when dealing with large datasets or when you want to ensure unique results directly from the database. By using SELECT DISTINCT, you can reduce the amount of data transferred between the database and PHP, resulting in better performance. On the other hand, using array_unique() in PHP requires fetching all the data first, which could be inefficient for large datasets.

// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=database', 'username', 'password');

// Query to select distinct values from a table
$query = "SELECT DISTINCT column_name FROM table_name";
$stmt = $pdo->query($query);

// Fetch the results
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Output the unique values
foreach ($results as $result) {
    echo $result['column_name'] . "\n";
}