0

No matter what I do, I can't seem to get json_encode to work on returned results from my fr column.

Here is an export of my table: mctrivia.com/language.zip

I set everything to utf8mb4 as suggested by my googling this problem but no luck.

If $language is 'en' and $fileID=1 this works. If $language is 'fr' and $fileID=1 this fails.

Either way the print_r gives proper results. The echo is showing null with fr.

    //get list of words in current language file
    $query='SELECT `key`,`' . $language . '` FROM `language` WHERE `file`=' . $fileID;
    $stmtGetWords = $conn->prepare($query);
    $stmtGetWords->bind_result($wordID,$text);
    $stmtGetWords->execute();
    $wordList=array();
      while ($stmtGetWords->fetch()) {
        $wordList[$wordID]=$text;
    }
    print_r($wordList);
    $stmtGetWords->close();

    //create json file data
    $fileData=json_encode($wordList);
    echo '<br>'. $fileData . '<br>';

Results from language en:

Array ( [lang] => English [subPage1Intro] => Welcome to sub page 1. ) 
{"lang":"English","subPage1Intro":"Welcome to sub page 1."}

Results from language fr:

Array ( [lang] => Fran�ais [subPage1Intro] => Bienvenue � sous page 1. ) 
Hemerson Varela
  • 24,034
  • 16
  • 68
  • 69
  • Could you please add the neccessary information within the question? In this case the result of `print_r`. – KhorneHoly Jan 13 '16 at 14:24
  • Results added to question. – Matthew Cornelisse Jan 13 '16 at 14:36
  • How did you get those two result? Print_r of what variable ? Also what is this method doing "$stmtGetWords->bind_result($wordID,$text);" – Nicolas Racine Jan 13 '16 at 14:37
  • 2
    JSON must be utf-8 encoded, the french text doesn't seems to be utf8. That's why you `can't get it to work` – KhorneHoly Jan 13 '16 at 14:41
  • @KhorneHoly: Was just about to say the same thing. It can't work if the ç isn't properly loaded or displayed! – Chris G Jan 13 '16 at 14:42
  • It loads and displays properly in phpMyAdmin. How can I make a form field send me utf8 then? – Matthew Cornelisse Jan 13 '16 at 14:55
  • `var_dump(json_last_error())`. don't assume the encoding succeeded. – Marc B Jan 13 '16 at 14:56
  • The way the french appears broken is a sure sign that it is ***not*** UTF-8 encoded. Check the duplicate for everything that can be set to UTF-8, most importantly everything database related. If still not solved, post details about your database connection here. – deceze Jan 13 '16 at 15:23
  • thanks. seems you can't enter values via phpMyAdmin. Created a page to enter data with `accept-charset="UTF-8"` in the form tag and it all works fine now. – Matthew Cornelisse Jan 13 '16 at 18:41

1 Answers1

0

Your french text doesn't contain valid JSON, because it's not UTF-8! You can find more information on the wikipedia page.

As soon as your JSON contains proper UTF-8, it'll work.

KhorneHoly
  • 4,666
  • 6
  • 43
  • 75