How can you filter out duplicate entries in a database using PHP?

To filter out duplicate entries in a database using PHP, you can use SQL queries to check for existing records before inserting new ones. One common method is to create a unique index on the column(s) that should not contain duplicates, which will prevent duplicate entries from being inserted. Another approach is to query the database before inserting a new record to check if a similar record already exists.

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

// Check if the record already exists
$stmt = $pdo->prepare("SELECT * FROM your_table WHERE column_name = :value");
$stmt->bindParam(':value', $value);
$stmt->execute();

if($stmt->rowCount() == 0) {
    // Insert the new record
    $insertStmt = $pdo->prepare("INSERT INTO your_table (column_name) VALUES (:value)");
    $insertStmt->bindParam(':value', $value);
    $insertStmt->execute();
    echo "Record inserted successfully.";
} else {
    echo "Record already exists.";
}