PHP UTF8不能正确显示中文字符 [英] PHP UTF8 not displaying chinese characters properly
问题描述
我试图用表格数据在php中回复一个中文单词,但它似乎没有正确显示
这是我的代码
<?php
';
echo'<?xml version =1.0encoding =utf-8?>
<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Strict // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
?>
< head>
< title>测试页< /标题>
< / head>
< body>
<?php
//插入连接字符串
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.0encoding =utf-8?>
。然后,重做你的代码,看起来像这样(符合HTML5)。在标题中保留 meta
属性的注释:
< ;! DOCTYPE html>
< html lang =zh-Hans>
< head>
< title>测试页< /标题>
< meta charset =utf-8>
< / head>
< body>
<?php
//从这里开始PHP代码
$ value =黄后乎;;
echo $ value;
?>
< / body>
< / html>
注意:HTML lang
属性可以是用于声明网页的语言或网页的一部分。这是意在协助搜索引擎和浏览器。
编辑: 您必须指定SQL的结果字符编码通过指示MySQL服务器 BEFORE ,您的实际查询如下所示: I am trying to echo a chinese word in php from a table data but it seems it is not displaying properly Here's my code I have saved the source code into UTF-8 and yet it is not working properly, Instead of displaying the supposed text, it prints out Remove this line Note: The HTML EDIT: You have to specify the SQL`s result character encoding, by instructing the MySQL server, BEFORE your actual query, like this:
这篇关于PHP UTF8不能正确显示中文字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ 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');
//您的实际数据库查询
$ query = odbc_exec($ conn,SELECT * FROM member WHERE userid =。(int)$ ID);
//注意`(int)$ ID`部分,这是为了防止SQL注入攻击。
<?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>
???
echo'<?xml version="1.0" encoding="utf-8"?>
. Then, rework your code to look like this (HTML5 compliant). Keep the note of the meta
attribute in header:<!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>
lang
attribute can be used to declare the language of a Web page or a portion of a Web page. This is meant to assist search engines and browsers. $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.