How does the use of DISTINCT impact the efficiency of PHP code execution?

Using DISTINCT in a SQL query can impact the efficiency of PHP code execution because it requires the database to remove duplicate rows from the result set, which can be resource-intensive. To improve efficiency, it's recommended to only use DISTINCT when necessary and optimize the query to minimize the number of duplicate rows returned.

<?php
// Connect to database
$pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");

// Query with DISTINCT
$sql = "SELECT DISTINCT column FROM mytable WHERE condition = :condition";
$stmt = $pdo->prepare($sql);
$stmt->execute(['condition' => $condition]);

// Fetch results
$results = $stmt->fetchAll();

// Process results
foreach ($results as $row) {
    // Do something with each row
}
?>