我的数据库有简单的PHP测试API。它查询MySQL DB并返回JSON。问题是,有些值是西里尔字母,我得到的只是一个问号。我的PHP代码是这样的:
<?php
mysql_connect ( "localhost", "root", "" );
mysql_select_db ( "uni_database" );
$q = mysql_query ( "SELECT * FROM `students`" );
while ( $e = mysql_fetch_assoc ( $q ) )
$output [] = $e;
print (json_encode( $output ) );
mysql_close ();
?>
有谁知道如何将值从DB编码为UTF8?
答案 0 :(得分:1)
创建数据库时,请确保将其设置为utf8
CREATE DATABASE `myDb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
然后将字段类型设为文本
答案 1 :(得分:1)
mysql_connect ( "localhost", "root", "" );
mysql_select_db ( "uni_database" );
mysql_query("set character_set_connection=utf8");
mysql_query("set names utf8");
...