问题
$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