1

I know that there are lot of questions on this. I have tried many things but I couldn't fix it. Perhaps I failed executing the solutions because of my limited knowledge?

I select data from mysql database and use json_encode() function on it. This works , except for the data which contains special characters(Turkish). For those values , json returns null. How can I fix this?

Here is my simple php code:

<?php

require('init.php');

$sql="SELECT * FROM tablename;";

$result=mysqli_query($con,$sql);
$response=array();

while($row=mysqli_fetch_array($result)){


array_push($response,array("X"=>$row["x"],"Y"=>$row["y"]));

}

echo json_encode($response);


?>

in mysql , columns are set to utf8-turkish. I have tried things like setting headers , calling some functions , recreating php files in utf8 encoding etc.. but none did work.

Rick James
  • 135,179
  • 13
  • 127
  • 222
  • Have you set the mysqli charset http://php.net/manual/en/mysqli.set-charset.php – RiggsFolly Aug 02 '16 at 22:57
  • 2
    Possible duplicate of [json\_encode function: special characters](http://stackoverflow.com/questions/20694317/json-encode-function-special-characters) – scrowler Aug 02 '16 at 22:57
  • Hello Riggs . no I didn't try that yet. Do I need to write that in my connection script(init.php)? –  Aug 02 '16 at 23:00
  • 1
    Riggs I did use it now. For a special character , instead of getting null , I get this : "\u00d6" . Does this mean it worked? –  Aug 02 '16 at 23:15

2 Answers2

2

Using mysqli_set_charset function in my connection script solved the problem. Couldn't find this on similar questions. Thanks to user "RiggsFolly" .

-3

Add TRUE: json_decode($string, TRUE); That will get you utf8 characters instead of Unicode escape sequences (like \u00d6).

You need utf8 (or utf8mb4) in the connection, in the table, and in the html. See this.

Community
  • 1
  • 1
Rick James
  • 135,179
  • 13
  • 127
  • 222