php mySQL 表中的重音字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14083847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:35:41  来源:igfitidea点击:

Accented characters in mySQL table

phpmysqlunicodeutf-8non-ascii-characters

提问by s427

I have some texts in French (containing accented characters such as "é"), stored in a MySQL table whose collation is utf8_unicode_ci (both the table and the columns), that I want to output on an HTML5 page.

我有一些法语文本(包含重音字符,例如“é”),存储在排序规则为 utf8_unicode_ci(表和列)的 MySQL 表中,我想在 HTML5 页面上输出这些文本。

The HTML page charset is UTF-8 (< meta charset="utf-8" />) and the PHP files themselves are encoded as "UTF-8 without BOM" (I use Notepad++ on Windows). I use PHP5 to request the database and generate the HTML.

HTML 页面字符集是 UTF-8(< meta charset="utf-8" />),PHP 文件本身被编码为“没有 BOM 的 UTF-8”(我在 Windows 上使用 Notepad++)。我使用 PHP5 请求数据库并生成 HTML。

However, on the output page, the special characters (such as "é") appear garbled and are replaced by "?".

但是,在输出页面上,特殊字符(如“é”)出现乱码并被“?”替换。

When I browse the database (via phpMyAdmin) those same accented characters display just fine.

当我浏览数据库(通过 phpMyAdmin)时,这些相同的重音字符显示得很好。

What am I missing here?

我在这里缺少什么?

(Note: changing the page encoding (through Firefox's "web developer" menu) to ISO-8859-1 solves the problem... except for the special characters that appears directly in the PHP files, which become now corrupted. But anyway, I'd rather understand why it doesn't work as UTF-8 than changing the encoding without understanding why it works. ^^;)

(注意:将页面编码(通过 Firefox 的“Web 开发人员”菜单)更改为 ISO-8859-1 可以解决问题......除了直接出现在 PHP 文件中的特殊字符,这些字符现在已损坏。但无论如何,我宁愿理解为什么它不能作为 UTF-8 工作,也不愿在不了解其工作原理的情况下更改编码。^^;)

回答by Netorica

I experienced that same problem before, and what I did are the following

我之前遇到过同样的问题,我所做的是以下

1) Use notepad++(can almost adapt on any encoding) or eclipse and be sure in to save or open it in UTF-8 without BOM.

1)使用记事本++(几乎可以适应任何编码)或eclipse,并确保在没有BOM的情况下以UTF-8保存或打开它

2) set the encoding in PHP header, using header('Content-type: text/html; charset=UTF-8');

2)在PHP标头中设置编码,使用 header('Content-type: text/html; charset=UTF-8');

3) remove any extra spaces on the start and end of my PHP files.

3) 删除 PHP 文件开头和结尾的任何多余空格。

4) set all my table and columns encoding to utf8mb4_general_cior utf8mb4_unicode_civia PhpMyAdmin or any mySQL client you have. A comparison of the two encodings are available here

4) 将我所有的表和列编码设置为utf8mb4_general_ciutf8mb4_unicode_ci通过 PhpMyAdmin 或您拥有的任何 mySQL 客户端。两种编码的比较可在此处获得

5) set mysql connection charset to UTF-8 (I use PDO for my database connection )

5)将mysql连接字符集设置为UTF-8(我的数据库连接使用PDO)

  PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
  PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET utf8"

or just execute the SQL queries before fetching any data

或者在获取任何数据之前执行 SQL 查询

6) use a meta tag <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

6) 使用元标记 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

7) use a certain language code for French <meta http-equiv="Content-language" content="fr" />

7) 使用某种语言代码为法语 <meta http-equiv="Content-language" content="fr" />

8) change the html element lang attribute to the desired language

8) 将 html 元素的 lang 属性更改为所需的语言

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">

and will be updating this more because I really had a hard time solving this problem before because I was dealing with Japanese characters in my past projects

并将对此进行更多更新,因为我之前真的很难解决这个问题,因为我在过去的项目中处理日语字符

9) Some fonts are not available in the client PC, you need to use Google fontsto include it on your CSS

9) 某些字体在客户端 PC 中不可用,您需要使用Google 字体将其包含在您的 CSS 中

10) Don't end your PHP source file with ?>

10) 不要以 PHP 源文件结尾 ?>

NOTE:

笔记:

but if everything I said above doesn't work, try to adjust your encoding depending on the character-set you really want to display, for me I set everything to SHIFT-JISto display all my japanese characters and it really works fine. But using UFT-8must be your priority

但是如果我上面说的一切都不起作用,请尝试根据您真正想要显示的字符集调整您的编码,对我来说,我将所有内容都设置SHIFT-JIS为显示我所有的日语字符,它确实可以正常工作。但使用UFT-8必须是你的首要任务

回答by Vucko

This works for me

这对我有用

  1. Make your database utf8_general_ci
  2. Save your files in N++ as UTF-8 without BOM
  3. Put $mysqli->query('SET NAMES utf8');after the connection to the database in your PHP file
  4. Put < meta charset="utf-8" />in your HTML-s
  1. 制作你的数据库 utf8_general_ci
  2. 将您的文件保存在 N++ 中 UTF-8 without BOM
  3. $mysqli->query('SET NAMES utf8');在 PHP 文件中连接到数据库之后放置
  4. 放入< meta charset="utf-8" />你的 HTML-s

Works perfect.

工作完美。

回答by Alain Tiemblo

If your php.inidefault_charsetis not set to UTF-8, you need to use a Content-typeto define your data. Apply the following header at the top of your file(s) :

如果您php.inidefault_charset未设置为 UTF-8,则需要使用 aContent-type来定义您的数据。在文件顶部应用以下标题:

header("Content-type: text/html; charset=utf-8");

If you have still troubles with encoding, the cause may be one of the following:

如果您仍然遇到编码问题,原因可能是以下之一:

  • a database server charset problem (check encoding of your server)
  • a database client charset problem (check encoding of your connection)
  • a database table charset problem (check encoding of your table)
  • a php default encoding problem (check default_encodingparameter in parameters.ini)
  • a multibyte missconfigured (see mb_string parameters in parameters.ini)
  • a <form>charset problem (check that it is sent as utf-8)
  • a <html>charset problem (where no enctype is set in your html file)
  • a Content-encoding:problem (where the wrong encoding is sent by Apache).
  • 数据库服务器字符集问题(检查服务器的编码)
  • 数据库客户端字符集问题(检查连接的编码)
  • 数据库表字符集问题(检查表的编码)
  • 一个php默认编码问题(检查default_encodingparameters.ini中的参数)
  • 多字节配置错误(请参阅 parameters.ini 中的 mb_string 参数)
  • 一个<form>字符集的问题(检查它是否为UTF-8发送)
  • 一个<html>字符集问题(在没有加密类型是在HTML文件中设置)
  • 一个Content-encoding:问题(Apache 发送了错误的编码)。

回答by Paul R

SET NAMES worked for me.

SET NAMES 为我工作。

My issue was in one of my editing pages the field with the foreign characters would not display, on the production web pages there was no problem.

我的问题是在我的一个编辑页面中,带有外来字符的字段不会显示,在生产网页上没有问题。

回答by itsols

I know you already have an answer. That's great. But strangely none of these answers solved my issue. I'd like to share my answer for the benefit of the others who may encounter the same issues.

我知道你已经有了答案。那太棒了。但奇怪的是,这些答案都没有解决我的问题。为了其他可能遇到相同问题的人的利益,我想分享我的答案。

I also had the same problems as the OP, with regards to French accents in a multi-lingual application.

关于多语言应用程序中的法语口音,我也遇到了与 OP 相同的问题。

But I encountered this issue for the first time when I had to pass (French accented) data as segments in AJAX calls.

但是当我不得不在 AJAX 调用中将(法语重音)数据作为段传递时,我第一次遇到了这个问题。

Yes, we must have the database set to work with UTF8. But the fact that AJAX calls had query strings (in my case segments, since I'm using CodeIgniter), I had to simply encode the French text.

是的,我们必须将数据库设置为使用 UTF8。但事实上 AJAX 调用有查询字符串(在我的案例中,因为我使用的是 CodeIgniter),我不得不简单地对法语文本进行编码。

To do this on the client-side, use the Javascript encodeURI() function with your data.

要在客户端执行此操作,请对您的数据使用 Javascript encodeURI() 函数。

And to reverse it in PHP, just use urldecode($MyStr)where data was received as parameters.

要在 PHP 中反转它,只需使用urldecode($MyStr)接收数据的位置作为参数。

Hope this helps.

希望这可以帮助。

回答by Xfile

  1. Type something full French signs in your (php) file
  2. Save that file as UTF-8
  3. Paste line beneath into your website header

    header('Content-Type: text/html; charset=utf-8');

  1. 在您的 (php) 文件中输入一些完整的法语符号
  2. 将该文件另存为 UTF-8
  3. 将下面的行粘贴到您的网站标题中

    header('Content-Type: text/html; charset=utf-8');

Page (file) should look good.

页面(文件)应该看起来不错。

If looks good go herefor mysql behavior after (SET_NAMES).

如果看起来不错,请(SET_NAMES) 之后查看 mysql 行为。