中文、PHP 和 MySQL 问题

发布于 2024-09-24 06:58:13 字数 704 浏览 3 评论 0原文

我有一个具有以下设置的 MySQL 数据库:
MySQL 字符集:UTF-8 Unicode (utf8)
MySQL连接排序规则:utf8_unicode_ci

我有一个表,其中有一列名为“softtitle”,该列以utf8_general_ci编码。该栏的条目包含中文字符。如果我通过 PHPMyAdmin 控制面板运行 SQL,中文字符会正确显示。但是如果我通过PHP文件运行SQL,所有汉字都显示错误。这是 PHP 文件:

<?php
header("Content-Type: text/html; charset=utf8_general_ci"); 

mysql_connect("116.123.163.73","xxdd_f1","xxdd123"); // host, username, password
mysql_select_db("xxdd");

mysql_query("SET names 'utf8_general_ci'"); 

$q = mysql_query("SELECT  softtitle
FROM  dede_ext
LIMIT 0 , 30");

while($e = mysql_fetch_assoc($q))
        $output[] = $e;

print(json_encode($output));

mysql_close();
?>

这里有什么问题?我应该怎么做才能解决这个问题?非常感谢!

I have a MySQL database with the following settings:
MySQL charset: UTF-8 Unicode (utf8)
MySQL connection collation: utf8_unicode_ci

I have a table with a column named "softtitle", this column is coded in utf8_general_ci. The entries of this column contain Chinese characters. If I run SQL through the PHPMyAdmin Control pane, the Chinese characters are shown correctly. But if I run SQL through a PHP file, all Chinese characters are shown wrongly. Here is the PHP file:

<?php
header("Content-Type: text/html; charset=utf8_general_ci"); 

mysql_connect("116.123.163.73","xxdd_f1","xxdd123"); // host, username, password
mysql_select_db("xxdd");

mysql_query("SET names 'utf8_general_ci'"); 

$q = mysql_query("SELECT  softtitle
FROM  dede_ext
LIMIT 0 , 30");

while($e = mysql_fetch_assoc($q))
        $output[] = $e;

print(json_encode($output));

mysql_close();
?>

What is wrong here? What should I do to fix this problem? Thank you very much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心舞飞扬 2024-10-01 06:58:13

你的标题错了。您不应该将其设置为表/数据库的字符集。

header("Content-Type: text/html; charset=UTF-8");

这同样适用于“SET NAMES”:

mysql_query("SET names 'utf8'"); 

最后,您要打印 json 编码数据,您的 Content-type 不应该是 text/html 而是 <代码>应用程序/json。

You header is wrong. You're not supposed to set it to the character set of the table/database.

header("Content-Type: text/html; charset=UTF-8");

The same applies for "SET NAMES":

mysql_query("SET names 'utf8'"); 

And as a last thing, you are printing out json encoded data, your Content-type shouldn't be text/html but application/json.

够运 2024-10-01 06:58:13

因为这个问答在 Google 搜索结果中仍然排名靠前..

暂时搁置从在 php 中使用 mysql 语句切换到使用 mysqli 的一般建议,将其替换

mysql_connect("116.123.163.73","xxdd_f1","xxdd123");
mysql_select_db("xxdd");

为:

$con = mysql_connect("116.123.163.73","xxdd_f1","xxdd123");
mysql_set_charset($con, 'utf8');
mysql_select_db("xxdd");

检查您的数据库,或者至少检查该列,确实被整理为 utf8_general_ci - 尽管

如果您的 php 生成纯 JSON 输出,您可能会使用 utf8mb4_unicode_ci 获得更好的结果,例如,您通过 AJAX 调用将数据提取到另一个文档中获取的 JSON“对象”,您应该在 PHP 文件中使用的标头是
标头(“内容类型:application/json”,true);

而不是 text/html 或其他任何内容的标头内容类型。

最后,假设您最终将从数据库中获取的值呈现在 html 文档上并将其放入 JSON 中,请记住以以下内容开始该文档:

<!DOCTYPE html>
<html lang="zh-Hans">
<head>
    <meta charset="utf-8">
...etc

注意标头元中的 utf-8 声明,以及语言的声明(我上面的代码中的简体中文 = zh-Hans)。如果您使用的是从右到左编写的脚本,例如。阿拉伯文脚本,也将 dir="rtl" 添加到标签中。

because this Q&A still ranks highly in Google search results..

setting aside for a moment the general advice to switch from using mysql statements in php to using mysqli, replace this:

mysql_connect("116.123.163.73","xxdd_f1","xxdd123");
mysql_select_db("xxdd");

with this:

$con = mysql_connect("116.123.163.73","xxdd_f1","xxdd123");
mysql_set_charset($con, 'utf8');
mysql_select_db("xxdd");

check that your database, or at least the column, really IS collated as utf8_general_ci - though you might get better results with utf8mb4_unicode_ci

if your php is producing a purely JSON output, for example, a JSON 'object' that you're picking up with an AJAX call to pull data into another document, the header you should use in the PHP file is
header("Content-Type: application/json", true);

and not a header content-type of text/html or anything else.

and finally, assuming you're eventually presenting the values taken from the DB and placed into your JSON onto an html document, remember the to start that document with the following:

<!DOCTYPE html>
<html lang="zh-Hans">
<head>
    <meta charset="utf-8">
...etc

note the declaration of utf-8 in the meta of the head, and the declaration of the language (chinese simplified in my code above = zh-Hans). If you are using a script which is written from right to left, eg. arabic sccripts, add dir="rtl" into the tag as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文