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€
[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€
[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-? ??.??????? ??????...
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: