中文简体字符显示为问号钻石

时间:2017-01-04 14:16:59

标签: php html mysql mysqli character-encoding

我已设置meta并检查mysql似乎已经在utf 8上了,这似乎是什么问题?

HTML

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

enter image description here

PHP DB连接

$DB_NAME = 'ssl';
$DB_HOST = 'localhost';
$DB_USER = 'dbuser';
$DB_PASS = 'dbpass';

$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);


// $mysqli->character_set_name();
mysqli_set_charset($mysqli,"utf8");


if (mysqli_connect_errno())
{
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

查询输出

$query = $mysqli->query("select {$translate_to} as lang from locale_cn where {$translate_from} = '$phrase' ");
        $row = mysqli_fetch_array($query);

显示

<h1><?php echo translate("In-Kind Donation"); ?></h1>

尝试设置标题('Content-Type:text / html; charset = UTF-8'),但结果仍然相同

enter image description here

编辑:添加翻译功能信息

 function translate($phrase) {

    if(!isset($_SESSION)) 
    { 
        session_start(); 
    }

    require 'db/mysql.php';

        $_GET['lang'] = $_SESSION['lang'];
        $lang_session = $_GET["lang"];

        if($lang_session == 'CN') {
            $translate_from = 'EN';
            $translate_to = 'CN';
        } 
        else {
            $translate_from = 'CN';
            $translate_to = "EN";
        } 

        $query = $mysqli->query("select {$translate_to} as lang from locale_cn where {$translate_from} = '$phrase' ");
        $row = mysqli_fetch_array($query);

        if(isset($row['lang']) && $row['lang'] !== ""){

            return $row['lang'];
        }
        else{
           return $phrase;
        }
    }

2 个答案:

答案 0 :(得分:1)

有些中文字符需要4个字节进行编码。 MySQL的CHARACTER SET uf8以3字节编码停止。更改为utf8mb4

mysqli_set_charset($mysqli,"utf8");
mysqli_set_charset($mysqli,"utf8mb4");

您的<meta ... charset=UTF-8" />标记正确无误。

(这也适用于表情符号。)

答案 1 :(得分:0)

您可能还需要设置mb_internal_encoding("UTF-8");