[英]Accents in php and mysql pdo
I need to read some values from DB in which I have accents in this way: "CafeterÃa" which real word is "Cafetería" 我需要从数据库中读取一些我有这种口音的值:“CafeterÃa”,其真实词是“Cafetería”
When I pass the json to the php script, it is like this: 当我将json传递给php脚本时,它是这样的:
{"attribution":"CafeterÃa"}
$json = file_get_contents('php://input');
$json_post = json_decode($json, true);
$attribution = $json_post['attribution'];
But it is read wrong and doesn't give me any row from DB. 但这是错误的,并且没有给我DB的任何信息。 I use PDO to read from DB. 我使用PDO从数据库读取。
$db = new PDO("mysql:host=$hostDB;dbname=$nameDB;charset=UTF-8", $userDB, $passwordDB, array(PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
$stmt = $db->prepare("SELECT id, title, lat, lon FROM POI_Table WHERE attribution=:attribution");
$stmt->execute(array(':attribution'=>$attribution));
If I execute the same query in HEIDI but writing the value exactly as json file gives it, it work. 如果我在HEIDI中执行相同的查询,但将值恰好写成json文件给出的值,那么它将起作用。 How should I convert the value before using in query? 在查询中使用之前,应如何转换值?
在您的代码中如何做呢?
$json_post = json_decode(utf8_decode($json), true);
you are setting encoding for mysql wrong way. 您为mysql设置编码方式错误。
It's utf8
without a dash 它是utf8
没有破折号
the other answer is essentially wrong as it causes double extra recoding and works for lat1 characters only. 另一个答案本质上是错误的,因为它会导致双重多余的重新编码,并且仅适用于lat1字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.