mySQL表中的重音字符 [英] Accented characters in mySQL table

查看:173
本文介绍了mySQL表中的重音字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些法文文本(包含带重音符号的é"),存储在MySQL表中,该表的排序规则为utf8_unicode_ci(表和列),我想在HTML5页面上输出该文本.

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

但是,在输出页面上,特殊字符(例如é")显示为乱码,并被替换为.".

当我浏览数据库(通过phpMyAdmin)时,那些带有重音符号的字符显示得很好.

我在这里想念什么?

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

解决方案

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

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

2)使用header('Content-type: text/html; charset=UTF-8');

在PHP标头中设置编码

3)删除PHP文件开头和结尾的所有多余空格.

4)通过PhpMyAdmin或您拥有的任何mySQL客户端将我所有的表和列编码设置为utf8mb4_general_ciutf8mb4_unicode_ci.可以在此处

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

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

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

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

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

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

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

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

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

10)不要以?>

结束PHP源文件

注意:

但是,如果我上面所说的所有内容都不起作用,请尝试根据您真正想要显示的字符集来调整编码,对我来说,我将所有内容都设置为SHIFT-JIS以显示我所有的日语字符,并且它确实有效美好的.但是必须优先使用UFT-8

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.

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.

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.

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. ^^;)

解决方案

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.

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

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

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

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

  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

6) use a meta tag <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" />

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

<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 fonts to include it on your CSS

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

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-JIS to display all my japanese characters and it really works fine. But using UFT-8 must be your priority

这篇关于mySQL表中的重音字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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