1

I have a problem with my script which request records from database by AJAX, and then return it back via JSON. It works normally on my localhost but when I upload it to server it's doesn't work.

I read topics about empty json_encode() result, and found that similar problem was for many people and there was solution with data encoding. But all my files, tables, and data normally in UTF-8 encoding both locally and on the remote server.

All what do my PHP script is request records from database (I use PDO library, and set database encoding to UTF-8 - SET NAMES 'utf8') then throw the loop it generates HTML tags with data from database and then return back as array with json_encode($result).

If I'll try to print_r($result) and watch them in browser Developer tools I will see correct array with normall data. But if I'll return them with json_encode($result) the result will be nothing.

Also I have Russian text in my result array.

Example of results array:

[data] => Array
    (
        [0] => Array
            (
                [0] => <input type="checkbox" name="id[]" value="1">
                [1] => 1
                [2] => Английский стол
                [3] => <span title='Вращающийся английский стол с выдвижными ящиками.Англия, 1900-е гг.Красное дерево, мягкая кожа диска, передвигается на бронзовых колесиках.Диаметр 91, высота 71 см.'>Вращающийся английский стол с выдвижными ящиками.<br>Англия, 1900-е гг.<br>Красное дерево, мягкая кожа диска, перед...</span>
                [4] => 250&euro;
                [5] => sss
                [6] => 
                [7] => <span class="label label-sm label-success">Опубликован</span>
                [8] => <a href="/jc_adm/?p=edit_product&id=1" class="btn btn-xs default btn-editable"><i class="fa fa-pencil"></i> Редактировать</a>
            )

        [1] => Array
            (
                [0] => <input type="checkbox" name="id[]" value="2">
                [1] => 2
                [2] => Старинный раскладной стол
                [3] => <span title='Оригинальный стол 1800 г. вторая половина.Изогнутые ножки, резная опора, натуральное дерево.Высота 125 Ширина 56 Высота 78 см.'>Оригинальный стол 1800 г. вторая половина.<br>Изогнутые ножки, резная опора, натуральное дерево.<br>Высота 125 Ширин...</span>
                [4] => 210&euro;
                [5] => sss
                [6] => 
                [7] => <span class="label label-sm label-success">Опубликован</span>
                [8] => <a href="/jc_adm/?p=edit_product&id=2" class="btn btn-xs default btn-editable"><i class="fa fa-pencil"></i> Редактировать</a>
            )

        [2] => Array......

But if I manualy create array with, for example next infromation echo json_encode(array('fruits' => array('banan', 'apple', 'mango', 'редиска')));.

It will work correctly, and will show normally JSON encoded string. But my array from example does not want to return. Why is this happening?

I found that if I remove setting database encoding $objDB->query("SET NAMES utf8");, I will get result but instead of russian text I get question marks ?????

{"data":[["<input type=\"checkbox\" name=\"id[]\" value=\"1\">",1,"?????????? ????","<span title='??????????? ?????????? ???? ? ?????????? ???????.??????, 1900-? ??.??????? ??????...

3 Answers 3

4

Try this one, it will definitely works

function utf8ize($d) {
  if (is_array($d)) {
     foreach ($d as $k => $v) {
       $d[$k] = utf8ize($v);
     }
  } else if (is_string ($d)) {
     return utf8_encode($d);
  }
   return $d;
}

and then call

echo json_encode( utf8ize( $array ) );
Sign up to request clarification or add additional context in comments.

1 Comment

Yes this works fine, but the reason why we need to hardcode this function is obscure however. PHP developers should embed this option in the command's parameters!
3

Your json_encode seems to be failing, you would have gotten the notice if you had it enabled in your PHP config. There is other way of deciding is your json_encode failed, try getting the value of json_last_error

This error message might be helpful to decide what went wrong with the encoding.

EDIT

Questions like this has already been answered:

Here

1 Comment

Hello, Arun! It's return to me int 5, and the same return on localhost. But on localhost it's works in contrast with remote server. :(
0

Try

json_encode($result, JSON_UNESCAPED_UNICODE);

Check 2nd parameter of json_encode

1 Comment

Hello, Jigar! Yeah, i tryed to do that, but result is same. :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.