I'm trying to write a PHP script that loops through a CSV and inserts the values from the CSV to a MySQL table. The characters look fine in the CSV, but when I insert them into the database, they show up asç‹‚å¹²
and variants.
I have the PDO connection set to a UTF-8 charset, the table itself is set to UTF-8, and when I dump the characters, they show as the correct Chinese characters, as long as I set the UTF-8 header. However, they go all wonky on insert, and remain so when selecting them back out. If I use a GUI (Navicat) to insert a particular phrase, it selects out fine.
$mysql = new PDO('mysql:host=1.2.3.4;dbname=db;charset=UTF-8', 'username', 'password');
header('Content-type: text/html; charset=utf-8');
$handle = fopen('bannedWords.csv', 'r');
$count = 0;
$word = '';
$insert = $mysql->prepare("INSERT INTO sensitivePhrases SET phrase = :word");
$insert->bindParam(':word', $word);
while (($data = fgetcsv($handle)) !== false) {
$word = $data[1];
var_dump($word);
$insert->execute();
}
I've tried using uft8_encode
and utf8_decode
, as well as mb_convert_encoding
, none of which have helped. I'd love some advice on where I'm going wrong.