简体   繁体   English

在 MySQL 数据库中保存口音

[英]Save Accents in MySQL Database


I'm trying to save French accents in my database, but they aren't saved like they should in the DB.我正在尝试将法语口音保存在我的数据库中,但它们并没有像在数据库中那样保存。
For example, a "é" is saved as "é".例如,“é”被保存为“é”。
I've tried to set my files to "Unicode (utf-8)", the fields in the DB are "utf8_general_ci" as well as the DB itself.我试图将我的文件设置为“Unicode (utf-8)”,数据库中的字段是“utf8_general_ci”以及数据库本身。
When I look at my data posted through AJAX with Firebug, I see the accent passed as "é", so it's correct.当我使用 Firebug 查看通过 AJAX 发布的数据时,我看到重音为“é”,所以它是正确的。

Thanks and let me know you need more info!谢谢,让我知道您需要更多信息!

Personally I solved the same issue by adding after the MySQL connection code : 我个人通过添加MySQL连接代码后解决了同样的问题:

mysql_set_charset("utf8");

or for mysqli: 或者对于mysqli:

mysqli_set_charset($conn, "utf8");

or the mysqli OOP equivalent: 或mysqli OOP等价物:

$conn->set_charset("utf8");

And sometimes you'll have to define the main php charset by adding this code: 有时您必须通过添加以下代码来定义主要的php charset

mb_internal_encoding('UTF-8');

On the client HTML side you have to add the following header data : 在客户端HTML端,您必须添加以下标头数据:

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

In order to use JSON AJAX results (eg by using jQuery), you should define the header by adding : 为了使用JSON AJAX结果(例如使用jQuery),您应该通过添加以下内容来定义标头:

header("Content-type: application/json;charset=utf8");
json_encode(
     some_data
);

This should do the trick 这应该可以解决问题

The best bet is that your database connection is not UTF-8 encoded - it is usually ISO-8859-1 by default. 最好的办法是您的数据库连接不是UTF-8编码 - 默认情况下通常是ISO-8859-1。

Try sending a query 尝试发送查询

SET NAMES utf8;

after making the connection. 在建立连接后。

mysqli_set_charset($ conn,“utf8”);

如果你使用PDO,你必须像那样实现:

new \PDO("mysql:host=$host;dbname=$schema", $username, $password, array(\PDO::MYSQL_ATTR_INIT_COMMAND =>  'SET NAMES utf8') );

Have you reviewed http://dev.mysql.com/doc/refman/5.0/en/charset-unicode.html : 你有没有看过http://dev.mysql.com/doc/refman/5.0/en/charset-unicode.html

Client applications that need to communicate with the server using Unicode should set the client character set accordingly; 需要使用Unicode与服务器通信的客户端应用程序应相应地设置客户端字符集; for example, by issuing a SET NAMES 'utf8' statement. 例如,通过发出SET NAMES'utf8'语句。 ucs2 cannot be used as a client character set, which means that it does not work for SET NAMES or SET CHARACTER SET. ucs2不能用作客户端字符集,这意味着它不适用于SET NAMES或SET CHARACTER SET。 (See Section 9.1.4, “Connection Character Sets and Collations”.) (请参见第9.1.4节“连接字符集和排序”。)

Further to that: 除此之外:

if you get data via php from your mysql-db (everything utf-8) but still get '?' 如果你从你的mysql-db(一切都是utf-8)通过php获取数据但仍然得到'?' for some special characters in your browser (), try this: 对于浏览器中的某些特殊字符(),请尝试以下方法:

after mysql_connect() , and mysql_select_db() add this lines: mysql_query("SET NAMES utf8"); 在mysql_connect()和mysql_select_db()之后添加以下行:mysql_query(“SET NAMES utf8”);

worked for me. 为我工作。 i tried first with the utf8_encode, but this only worked for äüöéè... and so on, but not for kyrillic and other chars. 我首先尝试使用utf8_encode,但这只适用于äüöéè......等等,但不适用于kyrillic和其他字符。

Use UTF8: 使用UTF8:

Set a meta in your 在你的中设置元

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

When you connect to your mySQL DB, force encoding so you DONT have to play with your mysql settings 当您连接到mySQL数据库时,强制编码,因此您不必使用您的mysql设置

$conn = mysql_connect('server', 'user', 'password') or die('Could not connect to mysql server.');
mysql_select_db('mydb') or die('Could not select database.');
mysql_set_charset('utf8',$conn); //THIS IS THE IMPORTANT PART

If you use AJAX, set you encoding like this: 如果您使用AJAX,请设置如下编码:

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

PHP(.net) advises against setting charsets after connecting using a query like SET NAMES utf8 because your functionality for escaping data inside MySQL statements might not work as intended. PHP(.net)建议在使用SET NAMES utf8之类的查询进行连接后设置字符集,因为在MySQL语句中转义数据的功能可能无法正常工作。

Do not use SET NAMES utf8 but use the appropriate ..._set_charset() function (or method) instead, in case you are using PHP. 不要使用SET NAMES utf8,而是使用适当的... _ set_charset()函数(或方法),以防您使用PHP。

You need to a) make sure your tables are using a character encoding that can encode such characters (UTF-8 tends to be the go-to encoding these days) and b) make sure that your form submissions are being sent to the database in the same character encoding. 你需要a)确保你的表使用的字符编码可以编码这些字符(UTF-8往往是最近的编码)和b)确保你的表单提交被发送到数据库中相同的字符编码。 You do this by saving your HTML/PHP/whatever files as UTF-8, and by including a meta tag in the head that tells the browser to use UTF-8 encoding. 您可以通过将HTML / PHP /任何文件保存为UTF-8,并在头部包含一个告诉浏览器使用UTF-8编码的元标记来完成此操作。

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

Oh, and don't forget C, when connecting to the database, make sure you're actually using the correct character set by executing a SET NAMES charset=utf8 (might not be the correct syntax, I'll have to look up what it should be, but it will be along those lines) 哦,不要忘记C,当连接到数据库时,确保你通过执行SET NAMES charset = utf8实际使用正确的字符集(可能不是正确的语法,我将不得不查找什么它应该是,但它将沿着那些线)

Ok I have found a working solution for me:好的,我为我找到了一个可行的解决方案:

Run this mysql command运行此 mysql 命令

show variables like 'char%'; 

Here you have many variables: "character_set_server", "character_set_system" etc.这里有很多变量:“character_set_server”、“character_set_system”等。

In my case I have "é" for "é" in database and I want to show "é" on my website.就我而言,我在数据库中有“é”的“é”,我想在我的网站上显示“é”。

To work I have to change "character_set_server" value from "utf8mb4" to "latin1".为了工作,我必须将“character_set_server”值从“utf8mb4”更改为“latin1”。

All my correct value are:我所有的正确值是:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM