Theo Theo - 4 months ago 19
SQL Question

Greeks letters and JSON response

I try get data from my server which are written in Greek. They are stored fine,but when I read them I have a problem as shown.

[{"id":"22","game":"??? 3 - 0 ?????","date":"27th August, 2016"}]


I am adding the

JSON_UNESCAPED_UNICODE


inside my
json_encode()
function but know I get this.

Notice: Use of undefined constant JSON_UNESCAPED_UNICODE - assumed 'JSON_UNESCAPED_UNICODE' in /var/www/vhosts/theo-android.co.uk/httpdocs/ael/android/last_game_json.php on line 25

Warning: json_encode() expects parameter 2 to be long, string given in /var/www/vhosts/theo-android.co.uk/httpdocs/ael/android/last_game_json.php on line 25


This is my PHP code that reads the JSON.

<?php
include("../init.php");

$string="";
$newString="";
$get_posts = "select * from last_game";

error_reporting(E_ALL);
ini_set("display_errors", 1);

$run_posts = mysqli_query($con, $get_posts);

$posts_array = array();

while ($posts_row = mysqli_fetch_array($run_posts)) {
$row_array['id'] = $posts_row['id'];
$row_array['game'] = $posts_row['game'];
$row_array['date'] = $posts_row['date'];

array_push($posts_array,$row_array);
}

$string = json_encode($posts_array, JSON_UNESCAPED_UNICODE);
echo $string;


Any ideas how to fix it?

Thanks.

Theo.

EDIT

I did this but I get an error.

<?php
include("init.php");

$get_posts = "select * from last_game";
error_reporting(E_ALL);
ini_set("display_errors", 1);
$run_posts = mysqli_query($con,$get_posts);

$posts_array = array();

while ($posts_row = mysqli_fetch_array($run_posts)){


$row_array['id'] = $posts_row['id'];
$row_array['game'] =$posts_row['game'];
$row_array['date'] = $posts_row['date'];


array_push($posts_array,$row_array);

}
$str = '\u0391\u0395\u039b 3 - 0 \u039e\u03b1\u03bd\u03b8\u03b7';
$str = preg_replace_callback('/\\\\u([0-9a-fA-F]{4})/', function ($match) {
(return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
( }, $str);
//$string = json_encode(utf8_encode($posts_array));
//$response = utf8_encode($string,true);
//echo $response;
print($str);

?>


in this line:

return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');

Answer

Before you access your database, preferably right after the connection is set up, do this:

mysqli_query($con, "SET CHARACTER SET utf8");
mysqli_query($con, "SET NAMES 'utf8'");
mysqli_query($con, "SET SESSION collation_connection = 'utf8_general_ci'");

Also ensure that your table was created using UTF-8 encoding for text fields. To check this, use:

SHOW CREATE TABLE last_game;

If you see latin1 you'll want to change this to utf8, and utf8_general_ci or utf8_unicode_ci.

Also ensure that the data is in UTF-8 when it is input and stored.

Edit: It looks like you're getting escaped unicode from the JSON parsing. Probably because you have an older version of PHP. You can do this to convert it:

php > $str = '\u0391\u0395\u039b 3 - 0 \u039e\u03b1\u03bd\u03b8\u03b7';
php > $str = preg_replace_callback('/\\\\u([0-9a-fA-F]{4})/', function ($match) {
php (     return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
php ( }, $str);
php >
php > print($str);
ΑΕΛ 3 - 0 Ξανθη
php >

See here for other examples of how to emulate JSON_UNESCAPED_UNICODE in older PHP versions.

Edit: Change your edited code to this:

<?php 
include("../init.php");

// Probably just put this in init.php.
mysqli_query($con, "SET CHARACTER SET utf8");
mysqli_query($con, "SET NAMES 'utf8'");
mysqli_query($con, "SET SESSION collation_connection = 'utf8_general_ci'");

$string="";
$newString="";
$get_posts = "select * from last_game";

error_reporting(E_ALL); 
ini_set("display_errors", 1);

$run_posts = mysqli_query($con, $get_posts); 

$posts_array = array();

while ($posts_row = mysqli_fetch_array($run_posts)) {
    $row_array['id'] = $posts_row['id'];
    $row_array['game'] = $posts_row['game'];
    $row_array['date'] = $posts_row['date'];

    array_push($posts_array, $row_array);            
}

$string = json_encode($posts_array);
$string = preg_replace_callback('/\\\\u([0-9a-fA-F]{4})/', function ($match) {
    return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
}, $string);

echo $string;