What are some strategies to avoid importing duplicate data when reading from a CSV file and writing to a database using PHP?

When reading from a CSV file and writing to a database using PHP, one strategy to avoid importing duplicate data is to check if the data already exists in the database before inserting it. This can be done by querying the database with the data from the CSV file and only inserting the data if it does not already exist.

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

// Read data from CSV file
$csvFile = fopen('data.csv', 'r');
while (($data = fgetcsv($csvFile)) !== false) {
    // Check if data already exists in the database
    $stmt = $pdo->prepare("SELECT * FROM mytable WHERE column1 = ? AND column2 = ?");
    $stmt->execute([$data[0], $data[1]]);
    
    if ($stmt->rowCount() == 0) {
        // Insert data into the database
        $stmt = $pdo->prepare("INSERT INTO mytable (column1, column2) VALUES (?, ?)");
        $stmt->execute([$data[0], $data[1]]);
    }
}

// Close CSV file and database connection
fclose($csvFile);
$pdo = null;