Tuguldur
Tuguldur

Reputation: 83

JSON can't returning cyrillic data

$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?

Upvotes: 1

Views: 80

Answers (2)

Ajay
Ajay

Reputation: 235

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 );

Upvotes: 1

Saty
Saty

Reputation: 22532

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

Upvotes: 1

Related Questions