سؤال

I need to read some values from DB in which I have accents in this way: "Cafetería" which real word is "Cafetería"

When I pass the json to the php script, it is like this:

{"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. I use PDO to read from DB.

$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. How should I convert the value before using in query?

هل كانت مفيدة؟

المحلول

you are setting encoding for mysql wrong way.

It's utf8 without a dash

the other answer is essentially wrong as it causes double extra recoding and works for lat1 characters only.

نصائح أخرى

What about doing this in your code?

$json_post = json_decode(utf8_decode($json), true);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top