PHP UTF8不能正确显示中文字符 [英] PHP UTF8 not displaying chinese characters properly

查看:426
本文介绍了PHP UTF8不能正确显示中文字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用表格数据在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 ,您的实际查询如下所示:

  $ 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注入攻击。


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

<?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>

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 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>

Note: The 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.

EDIT:

You have to specify the SQL`s result character encoding, by instructing the MySQL server, BEFORE your actual query, like this:

$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.

这篇关于PHP UTF8不能正确显示中文字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆