1

I have a php file to return json but the result given not correctly.

Ex: Lập Trình Ứng Dụng in database => L?p Trình ?ng D?ng in view

Here it's php file

  <?php 
    $mysql = new mysqli('localhost','root','','freelancer');
    $result = $mysql->query("select * from tag");
    $rows = array();
    while($row = $result->fetch_array(MYSQL_ASSOC)) {
        $rows[] = array_map("utf8_encode", $row);
    }
    echo json_encode($rows);
    ?>

Hope everyone can help me with this.Thanks for read

  • Is your file saved in "UTF-8 without BOM"? – leonardo_palma Jan 30 '16 at 13:33
  • what is BOM? The Collation of the Tag table is utf8_unicode_ci.Every table i use that Collation but this is only table i encounter with utf8 errors –  Jan 30 '16 at 13:35
  • I'm talking about the file itself, it can be saved in differents formats, the one you need is "UTF-8 without BOM". Check with Notepad++ in "Encoding". – leonardo_palma Jan 30 '16 at 13:38
  • @leonardo_palma I'm using subline text 3.And the file it's tag.php.So do you think is there something wrong with the array_map("utf8_encode", $row); function ?.I'm following the code with this [article](http://nicolasbize.com/magicsuggest/tutorial-3.php) –  Jan 30 '16 at 13:54
  • @HoàngPhúcVũ he means when you open console in sublime text and type `view.encoding()` do you see `'UTF-8'` or `'UTF-8 with BOM'`. It is also possible that your charset in your connection is not set to `utf8`. check if this post help http://stackoverflow.com/questions/279170/utf-8-all-the-way-through – Andrew Jan 30 '16 at 14:19
  • @Andrew My tag.php file [here](http://prntscr.com/9wswiy) i'm already set utf8 but nothing change –  Jan 30 '16 at 14:30
  • can you try without the `"utf8_encode"` ?? I think if you encode an encoded string, it will mess up the encoding, and also check if your html charset is utf-8 – Andrew Jan 30 '16 at 14:41
  • @Andrew if i use $rows[] = array_map("utf8_encode", $row); i will have a result,but if i remove utf8_encode what should i replace with –  Jan 30 '16 at 14:49
  • you can try `array_map("htmlspecialchars", $arr);` since i am not sure if the string is safe or not(note )...or just echo the `$row` – Andrew Jan 30 '16 at 15:00
  • @Andrew thank you so much cheer :D –  Jan 30 '16 at 15:04
  • glad I helped a bit :), note it is best to set the encoding on `htmlspecialchars` since php changed the default encoding in php 5.4 and in 5.6 – Andrew Jan 30 '16 at 15:07

1 Answers1

0

utf8_encode() only works for strings that are iso-8895-1 encoded. As your string contains non-latin characters, your data can't be encoded in iso-8895-1, so utf8_encode() certainly won't work.

Instead, you need to ask MySQL to return its data encoded in UTF-8 already by adding set_charset("utf8") to your connection configuration. Now you have UTF-8 encoded strings, you no longer need to re-encode it.

Your new code should look like:

<?php 
    $mysql = new mysqli('localhost','root','','freelancer');
    $mysql->set_charset("utf8")

    $result = $mysql->query("select * from tag");
    $rows = array();
    while($row = $result->fetch_array(MYSQL_ASSOC)) {
        $rows[] = $row;
    }
    echo json_encode($rows);
?>
Alastair McCormack
  • 26,573
  • 8
  • 77
  • 100