JSON can't returning cyrillic data

旧巷老猫 提交于 2021-02-11 10:05:16

问题


$connect = mysqli_connect("localhost", "root", "123456", "json_test");

$sql = "SELECT * FROM names";
$query = mysqli_query( $connect,$sql );
while( $row=mysqli_fetch_array($query) ){
    $arr[]=array(
        'id' => $row['id'],
        'title' => $row['name']
    );
}
echo json_encode( $arr );

is returning ??????? and it's cyrillic text(utf-8). How to fix it?


回答1:


The mysqli_set_charset() function specifies the default character set to be used when sending data from and to the database server.

You need to add mysqli_set_charset($connect,"utf8"); after your connection!

$connect = mysqli_connect("localhost", "root", "123456", "json_test");
mysqli_set_charset($connect,"utf8");

Read http://php.net/manual/en/mysqli.set-charset.php




回答2:


Try adding $mysqli->set_charset("utf8"). Please check the below code and let me know

$connect = mysqli_connect("localhost", "root", "123456", "json_test");
$connect->set_charset("utf8");

$sql = "SELECT * FROM names";
$query = mysqli_query( $connect,$sql );
while( $row=mysqli_fetch_array($query) ){
    $arr[]=array(
        'id' => $row['id'],
        'title' => $row['name']
    );
}
echo json_encode( $arr );


来源:https://stackoverflow.com/questions/36571291/json-cant-returning-cyrillic-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!