Reputation: 145
I don't usually have issues on charset, but this one I'm not getting where the problem is.
function categories($host, $user, $pw, $bd){
$file_handle = fopen("categories.csv", "rb");
$mysqli = new mysqli($host, $user, $pw, $bd);
$mysqli->set_charset('utf8');
while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle);
$parts = explode(';', $line_of_text);
$description = $parts[1];
$parent = $parts[2];
$query = $mysqli->prepare("INSERT INTO categories (`description`, parent) VALUES(?, ?)");
$query->bind_param("si", $description, $parent);
$query->execute();
}
fclose($file_handle);
}
I do have the same code for others files, like countries, etc and I have no problem with charset.
As you can see I use, as always, the $mysqli->set_charset('utf8');
code. If I echo the $description
variable I do get the right characters (they are mostly portuguese).
This is what I've tried so far:
$query = $mysqli->prepare("SET NAMES 'utf8'"); // inside the function
$query->execute();
header('Content-Type: text/html; charset=utf-8'); // in the top of the page
But it continues to add in the database words like: Feij?o (Feijão), Ch?s (Chás).
I do find this stupid because I do insert in another table, like I said, countries names with accents without any problem.
Any guess?
Edit: Solved, using utf8_encode.
$query->bind_param("si", utf8_encode($description), $parent);
Upvotes: 0
Views: 62
Reputation: 17721
The problem probably is in file "categories.csv", which could not be UTF-8...
To convert the file from "ASCII" to UTF-8 you could use
iconv -f WINDOWS-1252 -t UTF-8 categories.csv
Upvotes: 2