使用 PHP 的 UTF-8 编码问题MySQL

发布于 2024-08-11 02:48:37 字数 287 浏览 7 评论 0原文

我将数据从 MySQL 4(它们最初设置为 latin2 编码)移至 MySQL 5,并将编码设置为 UTF-8。在 phpMyAdmin 中看起来不错,UTF-8 也可以。然而,网站上出现的不是一些字符,而是问号!网站编码也设置为UTF-8,所以不明白问题出在哪里。

PHP 和 HTML 文件也设置为 UTF-8。

我该如何解决这个问题?

I moved data from MySQL 4 (they were originally set to latin2 encoding) to MySQL 5 and set the encoding to UTF-8. It looks good in phpMyAdmin, and UTF-8 is okay. However, there are question marks instead of some characters on the website! The website encoding is also set to UTF-8, so I don’t understand where the problem is.

PHP and HTML files are also set to UTF-8.

How can I fix this?

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

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

发布评论

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

评论(11

刘备忘录 2024-08-18 02:48:37

尝试该查询。

SET NAMES utf8

在应用程序中的任何查询之前

Try the query

SET NAMES utf8

before any query in your application.

雨夜星沙 2024-08-18 02:48:37

在我的服务器上,将这些添加到我的 PHP 文件中没有任何效果:

ini_set('default_charset', 'utf-8');
mysql_set_charset('utf8');
header('Content-type: text/html; charset=utf-8');

但是,一旦我将其添加到 PHP 文件的顶部,一切都会正常运行:

$mysqli->query("SET NAMES 'utf8'");

注意:我在数据库中使用编码 utf8_general_ci ,但是 < code>utf8_unicode_ci 对我来说是一样的。

On my server, adding these to my PHP file had no effect:

ini_set('default_charset', 'utf-8');
mysql_set_charset('utf8');
header('Content-type: text/html; charset=utf-8');

But everything worked perfectly once I added this to the top of my PHP file:

$mysqli->query("SET NAMES 'utf8'");

Note: I am using encoding utf8_general_ci in my database, but utf8_unicode_ci works the same for me.

谜兔 2024-08-18 02:48:37

尝试将 MySQL 连接设置为 UTF-8:

SET NAMES 'utf8'

并发送显式 UTF-8 标头,以防万一您的服务器有其他默认设置:

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

Try setting the MySQL connection to UTF-8:

SET NAMES 'utf8'

And send explicit UTF-8 headers, just in case your server has some other default settings:

header('Content-type: text/html; charset=utf-8');
晨敛清荷 2024-08-18 02:48:37

您不必将 PHP 和 HTML 文件设置为 utf-8。

您只需将输出编码设置为 UTF-8,浏览器就会正确显示。

在 HTML 中:

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

在 PHP 中:

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

当您从 MySQL 表中获取 UTF-8 字符串时,除非您转换编码,否则它在浏览器输出中一直都是 UTF-8。这是浏览器解释它的方式。

You don't have to set your PHP and HTML files to utf-8.

You just have to set your output encoding to UTF-8 and the browser will display appropriately.

In HTML:

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

In PHP:

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

When you get a string that is UTF-8 from the MySQL table, it will be UTF-8 all the way to browser output unless you convert the encoding. It's the way that the browser interprets it.

十六岁半 2024-08-18 02:48:37

当您在网站上显示 UTF-8 字符,但告诉浏览器将它们解释为 拉丁语-1(或Latin-2)你看到这种乱码:àŸ

当您在网站上显示 Latin-1(或 Latin-2)字符,但告诉浏览器将它们解释为 UTF-8 时,您会看到问号。

所以我的猜测是,你将所有内容都切换为 UTF-8(我的意思是,你告诉数据库引擎、Web 服务器和浏览器你将使用 UTF-8),但你实际上并没有将字符串转换为 UTF-8 。

按照Darkerstar所说的去做。将转储转换为 UTF-8(Notepad++ 可以轻松做到这一点)并导入再来一次。

When you show UTF-8 characters on a website, but tell the browser to interpret them as Latin-1 (or Latin-2) you see this kind of gibberish: ß

When you show Latin-1 (or Latin-2) characters on a website, but tell the browser to interpret them as UTF-8, you see question marks.

So my guess is that you switched everything to UTF-8 (I mean, you told the database engine, the web server and the browser you would be using UTF-8), but you didn't actually convert the strings to UTF-8.

Do what Darkerstar said. Convert your dump to UTF-8 (Notepad++ can do that easily) and import it again.

久伴你 2024-08-18 02:48:37
mysql_query("SET NAMES UTF8");

在我的“connection.php”末尾添加这一行解决了我的问题。

我的连接文件的完整代码是:

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_test = "localhost";
$database_test = "test";
$username_test = "username";
$password_test = "password";
$test = mysql_pconnect($hostname_test, $username_test, $password_test) or trigger_error(mysql_error(), E_USER_ERROR);
mysql_query("SET NAMES UTF8");
?>

我的数据库排序规则是“utf8_general_ci”。

页面为“dreamweaver 默认 utf8”和“unicode 规范化形式=C(规范分解)”。

mysql_query("SET NAMES UTF8");

Adding this line at the end of my "connection.php" solved my problem.

My connection file's complete code is:

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_test = "localhost";
$database_test = "test";
$username_test = "username";
$password_test = "password";
$test = mysql_pconnect($hostname_test, $username_test, $password_test) or trigger_error(mysql_error(), E_USER_ERROR);
mysql_query("SET NAMES UTF8");
?>

My database collation is "utf8_general_ci".

Pages are "dreamweaver default utf8" and "unicode normalisation form=C (Canonical Decomposition)".

雄赳赳气昂昂 2024-08-18 02:48:37

我最近遇到了这个问题(我希望这和你遇到的问题是一样的),我尝试了很多方法,但最终有效的方法非常简单。

将转储的 SQL 文件转换为 UTF-8 格式,然后导入。

顺便说一句:我使用 Notepad++ 进行转换。

I had this problem recently (I hope it’s the same problem you are having), and I tried many ways, but at the end what worked was really simple.

Convert your dumped SQL file to UTF-8 format and then import it.

BTW: I used Notepad++ for the conversion.

故事和酒 2024-08-18 02:48:37

.htaccess 文件放入您的网站根目录中,内容如下:
AddDefaultCharset UTF-8

在连接到数据库后在 dbconfig 中设置:

mysql_query("SET NAMES 'utf8'");

Put a .htaccess file in your web-site root with content:
AddDefaultCharset UTF-8

and

in your dbconfig set after connection to the database:

mysql_query("SET NAMES 'utf8'");
十秒萌定你 2024-08-18 02:48:37

在 MySQL 中将每个 SQL数据库字段设置为 UTF-8 似乎还不够好。很烦人。

我最终强迫这个问题来解决编码问题:

我必须在打开数据库的每个地方使用这个:

$db->set_charset("utf8");

并且这有效。最后。

It doesn't seem like setting every SQL database, table, and field to UTF-8 in MySQL is good enough. Very annoying.

I ended up forcing the issue to solve encoding problems:

I had to use this every place I open the database:

$db->set_charset("utf8");

And that worked. Finally.

白龙吟 2024-08-18 02:48:37

这是一个修复。将标头设置为 header('Content-type: text/html; charset=utf-8'); 然后使用 utf8_decode($content) 打印您的内容。您必须同时拥有这两个才能使其发挥作用。

Here is a fix. Set the header to header ('Content-type: text/html; charset=utf-8'); Then print your content using utf8_decode($content). You must have the two to make it work.

友欢 2024-08-18 02:48:37

Putting:

$conn->query("SET NAMES 'utf8'");

其中 $conn 是我的 Mysqli 连接
在我的配置文件中有所帮助。

Putting:

$conn->query("SET NAMES 'utf8'");

Where $conn is my Mysqli connection
in my config file helped.

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