0

There are a lot of topics who discuss my problem but I cant still solve it.

I have a database which I encode by JSON. But when I put content with german char like ä, ö, ü etc the output is empty.

Here is my php script:

<?PHP
error_reporting(E_ALL);
ini_set('display_errors', '1');

//Verbindung zur Datenbank
include "inc_mysql.php";

//SQL-String
$select = "SELECT * FROM module ORDER BY name ASC";

//SQL-Befehl in Variable speichern
$sql = mysql_query($select);

//********************this one was missing!********************
mysql_set_charset("utf8");

//Array erstellen
$jsonArray = array();

while ($row = mysql_fetch_array($sql)){ 
    $jsonArray[] = $row;
}

echo json_encode($jsonArray, JSON_UNESCAPED_UNICODE);   
?>

The php file is UTF-8 without BOM.

The column is the dabase is utf8_general_ci.

BTW: The error reporting just says something about mysql_query() is depricated.

Might be a easy game for you guys :)

Solution found! Check the code!

Ero Stefano
  • 556
  • 2
  • 9
  • 27
  • 2
    Don't use the deprecated `mysql_*`-functions. They are deprecated since PHP 5.5 and completely removed in PHP 7. They are also insecure. Use MySQLi or PDO instead. – M. Eriksson Oct 03 '16 at 13:39
  • What happens if you output the data without converting it to json? Does it work then? A recommendation is to use `urf8mb4` as charset and `utf8mb4_unicode_ci` as collation instead of the old `utf8_general_ci` (mb = mutlibytes = more support for foreign chars and unicode has better sorting for more languages than general). Read more here: http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci – M. Eriksson Oct 03 '16 at 13:44
  • Thanks for your feedback. Without json I get an output but there is a questionmark in square. – Ero Stefano Oct 03 '16 at 13:53
  • Ah, Being a Swede, I recognize that. When you echo your json, make sure the browser is in utf-8 mode. I've noticed that I sometimes get those if the browser selects the wrong encoding (usually happens on var_dump()'ing json data) without the contents actually being wrong. Try to add some headers before outputting the data and see if it works better `header('Content-Type: application/json; charset=utf-8');` – M. Eriksson Oct 03 '16 at 13:58
  • @MagnusEriksson Thank you for your feedback. I did both but nothing is changing. :( – Ero Stefano Oct 03 '16 at 14:13

1 Answers1

0
Tung Nguyen
  • 767
  • 7
  • 6