如何从数据库中检索特殊字符?

发布于 2024-11-18 20:32:54 字数 316 浏览 2 评论 0 原文

我试图从 MySQL 数据库中检索特殊的希腊字符 µ 但在 HTML 中它是 显示此

我的 HTML 字符集: content="text/html; charset=windows-1252"

此外,在检索字符后,它还需要能够使用该字符在数据库中进行比较。

我尝试将 HTML 代码放入数据库中,例如 μ那么它就无法与数据库进行比较,因为 HTML 将其转换为 µ

我怎样才能正确地做到这一点?

I am trying to retrieve a special Greek character µ from a MySQL database but in HTML it is
showing this .

My HTML charset: content="text/html; charset=windows-1252"

In addition, after it retrieves the character it also need to be able to use the character to compare back in the database.

I've tried putting the HTML code in the database like μ; then it is not able to compare back to the database because HTML turns it into µ.

How can I do this appropriately?

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

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

发布评论

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

评论(4

百思不得你姐 2024-11-25 20:32:54

首先,我建议使用 utf8 作为字符集。

首先切换数据库的默认设置:

ALTER DATABASE db_name CHARACTER SET utf8;

接下来,要将表切换到 utf8,请应用此 SQL:

ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8;

您必须对每个表执行此操作。

最后,非常不幸的是,您必须知道转换在旧字符集(varchar、text 等)下创建的所有字符数据列。您必须首先转换为 BLOB 来执行此操作,以便已存储的字符不会被破坏:

alter table tbl_name change col_name col_name LONGBLOB;
alter table tbl_name change col_name col_name LONGTEXT CHARACTER SET utf8;

Wordpress 发布了有关如何执行此步骤的列类型指南:转换数据库字符集列

然后,最后更新 xhtml 中的字符集声明(如果是 html,则不带结尾 /):

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

这个过程很艰巨,但会转换数据库。使用 utf8 启动数据库要容易得多。

这一切都假设 MySQL 5,您可以查看 Mysql 5更改表参考。

First, I would recommend using utf8 as your charset.

Start by switching the default for the database:

ALTER DATABASE db_name CHARACTER SET utf8;

Next, to switch your tables over to utf8 apply this SQL:

ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8;

which you will have to do for each table.

Finally, and quite unfortunately, you have to know convert all the character data columns that were made under the old charset (varchar, text, etc.). You have to do this by way of first casting to a BLOB so that the characters already stored are not mangled:

alter table tbl_name change col_name col_name LONGBLOB;
alter table tbl_name change col_name col_name LONGTEXT CHARACTER SET utf8;

Wordpress publishes a column type by column type guide on how to do this step: converting database charset columns.

Then, finally update the charset declaration in your xhtml (or without the ending / if it is html):

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

This process is arduous, but will convert the database. It is much easier to start a db in utf8.

This is all assuming MySQL 5, for which you can see the Mysql 5 ALTER TABLE reference.

盛夏已如深秋| 2024-11-25 20:32:54

在包含特殊字符的表上尝试此操作(基于离开这篇文章):

ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8;

Try this on your table containing the special chars (based off of this post):

ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8;
伊面 2024-11-25 20:32:54

请参阅Shelhamer的答案,但您还需要将数据库连接设置为utf8。您可以在连接到数据库后立即使用mysql_set_charset('utf8');。或者在连接后运行以下查询:

SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8';

但是,我仍然建议您在网站上显示内容时使用 htmlentities() 以确保都是有效的 HTML。如果你混淆了编码,Firefox 往往会呕吐。

See Shelhamer's answer, but you also need to set the db connection to utf8. You can either use mysql_set_charset('utf8'); right after you connect to the DB. Alternatively run the following query after your're connected:

SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8';

however, i would still recommend you use htmlentities() when displaying your content on a website to make sure is's all valid HTML. Firefox tends to barf if you mix up the encoding.

や莫失莫忘 2024-11-25 20:32:54

在 php 中,在将其保存到数据库之前使用 utf8_encode()
它会解决问题

in php use utf8_encode() before saving it to the database.
It will solve the problem

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