PHP UTF8 无法正确显示中文字符


PHP UTF8 not displaying chinese characters properly

我正在尝试从表数据中回显php中的中文单词,但似乎它没有正确显示

这是我的代码

<?php
echo'<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-Hans" lang="zh-Hans">
';
?>
<head>
<title>A Test Page</title>
</head>
<body>
<?php
//Insert connection string
require_once 'confx/confx.php';
$ID = 222;
$conn = odbc_connect($odbc_dsn, $odbc_usr, $odbc_pwd);
if(!$conn) { die('Epic Fail!'); }
$query = odbc_exec($conn, "SELECT * FROM member WHERE userid = '$ID'");
$result = odbc_result($query, 'usernick');
echo $result;
odbc_free_result($query);
?>
</body>
</html>

我已经将源代码保存到 UTF-8 中,但它无法正常工作,它没有显示假定的文本,而是打印出???

echo'<?xml version="1.0" encoding="utf-8"?> 删除此行。然后,重新设计您的代码,使其看起来像这样(符合 HTML5)。在标题中记下meta属性:

<!DOCTYPE html>
<html lang="zh-Hans">
<head>
<title>A Test Page</title>
<meta charset="utf-8">
</head>
<body>
<?php
// Start PHP code from here
$value = "黄后乎";
echo $value;
?>
</body>
</html>

注: HTML lang 属性可用于声明网页或网页的一部分的语言。这是为了帮助搜索引擎和浏览器。

编辑:

您必须在实际查询之前通过指示MySQL服务器来指定SQL的结果字符编码,如下所示:

$query = odbc_exec($conn, "SET NAMES 'utf8'");
$query = odbc_exec($conn, "SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");
//Your actual DB query
$query = odbc_exec($conn, "SELECT * FROM member WHERE userid = ". (int) $ID); 
//Note the `(int) $ID` section, this is in order to prevent SQL injection attack.
<!DOCTYPE html>
<?php 
header('Content-Type: text/html; charset=utf-8');
?>
<html lang="zh-Hans">
<head>
<title>A Test Page</title>
    <head>
    <title>A Test Page</title>
    </head>
    <body>
    <?php
    $value = "黄后乎";
    echo $value;
    ?>

<!DOCTYPE html>
<html lang="zh-Hans">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>A Test Page</title>
    <head>
    <title>A Test Page</title>
    </head>
    <body>
    <?php
    $value = "黄后乎";
    echo $value;
    ?>