0

I have this data to datatable cell: AlgoVital Plus čćš so some characters is utf-8

Here's my database schema

enter image description here

After that I make json file with php and send it to fronted:

//header
header('Content-Type: text/html; charset=utf-8');
...
...
//at the end of php file:
 $jsonTable = json_encode($table);
    //echo $jsonTable;
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
    echo $jsonTable;

and HTML head is OK too:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">

but I cant show data with utf-8 characters... WHy?

JSON is not format with utf-8 characers... etc. where I have some characters like čćšž then I just get NULL at JSON file, when I use json encode

How I can solve this?

ErasmoOliveira
  • 1,416
  • 2
  • 20
  • 40
johny mile
  • 145
  • 1
  • 2
  • 11
  • possible duplicate of [json\_encode is returning NULL?](http://stackoverflow.com/questions/1972006/json-encode-is-returning-null) – Mike Mar 02 '15 at 18:22
  • http://i.imgur.com/oX6LIlt.png – johny mile Mar 02 '15 at 18:37
  • Why would you send text/html content type header when the content is application/json? – Mike Brant Mar 02 '15 at 18:39
  • hm, I dont know, I will delete this row – johny mile Mar 02 '15 at 18:41
  • @johnymile Now show some proof that the data was properly entered as UTF-8, a screenshot of the data in the DB. – Ruan Mendes Mar 02 '15 at 18:42
  • I am with @JuanMendes here in that show have not shown enough context/debugging info to determine if the problem is DB-related or JSON encoding related. PHP's json_encode assumes a UTF-8 character set, so typically there shuold not be a problem encoding UTF-8 characters to their JSON escape sequences. – Mike Brant Mar 02 '15 at 18:45
  • http://i.imgur.com/Vkb0R6O.png - please see last enter record, there is utf-8 characters – johny mile Mar 02 '15 at 18:45
  • OK. So your collations look OK, but what is the character set of the table? Can you past your `SHOW CREATE TABLE` information? Also, assuming this everything is correct with database set up, have you demonstrated that you can output these characters directly in response without JSON-encoding? In other words, what led you to believe that the problem lies in the JSON-encoding step? – Mike Brant Mar 02 '15 at 18:51
  • the problem was been with data, when I add it manualz then wont work, but when data comes from ajax when I add data, then works fine, THANKS VERZ MUCH FOR HELP – johny mile Mar 02 '15 at 21:05

2 Answers2

1

Try this

$table = '[{name: "PHP",version: 5.6}]'; //your json data
$jsonTable = json_encode($table, JSON_UNESCAPED_UNICODE);
$myObj = json_decode($jsonTable);
echo $myObj->name;
Abdullah Al Shakib
  • 2,034
  • 2
  • 15
  • 16
0

Try with this:

$table = utf8_encode($table);
$jsonTable = json_encode($table);
zamarrowski
  • 483
  • 2
  • 7