How can distinct values be used in SQL queries to avoid duplicate results in PHP?

To avoid duplicate results in SQL queries in PHP, you can use the DISTINCT keyword in your SELECT statement. This will ensure that only unique values are returned in the result set, eliminating any duplicates.

// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Query with DISTINCT keyword to avoid duplicates
$sql = "SELECT DISTINCT column_name FROM table_name";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Output data
    while($row = $result->fetch_assoc()) {
        echo "Value: " . $row["column_name"] . "<br>";
    }
} else {
    echo "0 results";
}

// Close connection
$conn->close();