How can PHP developers ensure that Umlauts and special characters are correctly preserved when importing .csv files into a MySQL database?

When importing .csv files into a MySQL database using PHP, developers can ensure that Umlauts and special characters are correctly preserved by setting the appropriate character encoding for both the PHP script and the MySQL connection. This can be achieved by using the `SET NAMES` query to specify the character set before importing the data. Additionally, developers should ensure that the .csv file itself is encoded in the correct character set, such as UTF-8, to avoid any encoding issues during the import process.

// Set the character encoding for PHP script
header('Content-Type: text/html; charset=UTF-8');

// Connect to the MySQL database with the appropriate character set
$mysqli = new mysqli("localhost", "username", "password", "database");
$mysqli->set_charset("utf8");

// Set the character encoding for the MySQL connection
$mysqli->query("SET NAMES 'utf8'");

// Import .csv file into MySQL database
if (($handle = fopen("data.csv", "r")) !== false) {
    while (($data = fgetcsv($handle, 1000, ",")) !== false) {
        $mysqli->query("INSERT INTO table_name (column1, column2) VALUES ('" . $mysqli->real_escape_string($data[0]) . "', '" . $mysqli->real_escape_string($data[1]) . "')");
    }
    fclose($handle);
}