php 在 MySQL 数据库中保存口音

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4854446/
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 16:05:54  来源:igfitidea点击:

Save Accents in MySQL Database

phpmysqlcharacter-encodingdiacritics

提问by Ebpo


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.
When I look at my data posted through AJAX with Firebug, I see the accent passed as "é", so it's correct.

Thanks and let me know you need more info!


我正在尝试将法语口音保存在我的数据库中,但它们并没有像在数据库中那样保存。
例如,“é”被保存为“??”。
我试图将我的文件设置为“Unicode (utf-8)”,数据库中的字段是“utf8_general_ci”以及数据库本身。
当我使用 Firebug 查看通过 AJAX 发布的数据时,我看到重音传递为“é”,所以它是正确的。

谢谢,让我知道您需要更多信息!

回答by Olivier Kaisin

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 charsetby adding this code:

有时您必须通过添加以下代码来定义主要的 php 字符集

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 (e.g. 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

这应该可以解决问题

回答by Pekka

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.

建立连接后。

回答by Zhanka

mysqli_set_charset($conn, "utf8");

回答by A.Baudouin

if you use PDO, you must instanciate like that :

如果你使用 PDO,你必须像这样实例化:

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

回答by Etienne Dupuis

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 DB 时,强制编码,这样您就不必使用您的 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');

回答by Shan Plourde

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; for example, by issuing a SET NAMES 'utf8' statement. ucs2 cannot be used as a client character set, which means that it does not work for SET NAMES or SET CHARACTER SET. (See Section 9.1.4, “Connection Character Sets and Collations”.)

需要使用 Unicode 与服务器通信的客户端应用程序应相应地设置客户端字符集;例如,通过发出 SET NAMES 'utf8' 语句。ucs2 不能用作客户端字符集,这意味着它不适用于 SET NAMES 或 SET CHARACTER SET。(请参阅第 9.1.4 节,“连接字符集和排序规则”。)

Further to that:

更进一步:

if you get data via php from your mysql-db (everything utf-8) but still get '?' for some special characters in your browser (), try this:

after mysql_connect() , and mysql_select_db() add this lines: 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.

如果您通过 php 从您的 mysql-db(所有 utf-8)获取数据,但仍然得到 '?' 对于浏览器中的一些特殊字符 (),试试这个:

在 mysql_connect() 和 mysql_select_db() 之后添加以下几行: mysql_query("SET NAMES utf8");

为我工作。我首先尝试使用 utf8_encode,但这仅适用于 ?ü?éè... 等,但不适用于 kyrillic 和其他字符。

回答by GordonM

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. 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.

您需要 a) 确保您的表格使用可以对此类字符进行编码的字符编码(如今 UTF-8 往往是首选编码)并且 b) 确保您的表单提交被发送到数据库中相同的字符编码。为此,您可以将 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 确保您实际上使用了正确的字符集(可能不是正确的语法,我必须查找什么它应该是,但它会沿着这些路线)

回答by Thomas

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。