I am working on a script that updates a database table (ENGINE=InnoDB DEFAULT CHARSET=latin1) from data obtained through a socket stream. The problem is that I have some strings that may contain French accented characters and that building an INSERT query gives me this error :
Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='
I've been trying different ways to replace these character with correctly encoded accented characters but I can only get ? and É. Anything else I've tried gave me the previous error message. I've changed my files encoding from ISO-8859-1 to UTF-8 to ANSI and nothing gives.
This is a sample code generating a query from the list I get where I try to replace the character É. At this point, I'm not sure whether it is a PHP or a MySQLi issue honestly.
$clients = $avantage->getListeClients(true, true);
$inserts = [];
foreach($clients AS $c) {
$c["archive"] = $c["archive"] == "F"?"0":"1";
$c["langue"] = in_array($c["langue"], ["T", "FR", "français"])?"FR":"EN";
if(stripos($c["nom"], "É") !== false) {
$lpos = 0;
$nom = "";
while($pos = stripos($c["nom"], "É", $lpos)) {
$nom .= utf8_encode(substr($c["nom"], $lpos, $pos - $lpos)).utf8_encode("É");
$lpos = $pos + 1;
}
$nom .= utf8_encode(substr($c["nom"], $lpos));
$c["nom"] = $nom;
echo $nom."<br />";
}
$inserts[] = "id_client = \"".$c["id"]."\", nom = \"".$c["nom"]."\", langue = \"".$c["langue"]."\", archive = \"".$c["archive"]."\"";
}
$query = "INSERT INTO clients (id_client, nom, langue, archive) VALUES (".implode("),(", $inserts).") ON DUPLICATE KEY UPDATE archive = values(archive)");