本文介绍了编码:一切都是UTF-8,但DB输出显示错误。有任何想法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我知道,有很多关于编码的问题,但经过很多检查,不幸的是,这个问题仍然存在。

I know, there are plenty of questions about encoding but after checking a lot, the problem unfortunately still exists.

我导出了PHP / HTML文件)和从笔记本到PC的MySQL转储(zip)(不使用git)。每个文件都是 UTF-8 ,每个字母都会显示在编辑器和数据库(PHPMyAdmin)中。

I exported the PHP/HTML-files (zip) and the MySQL-Dump (zip) from a Notebook to a PC (without using git). Every file is UTF-8 and every letter is displayed right in the Editor and in the database (PHPMyAdmin).

但是当我打开在任何浏览器中的项目中,显示一些字母(ä,ü,ü,...)。

But when I open the project in any Browser, some letters (ä, ü, ü, ...) are displayed wrong.

在HTML头中,我有这行设置字符集:

In the HTML head I've this line for setting the charset:

<meta charset="utf-8">

使用这一行,硬编码的字母显示正确,但数据库中的值显示错误。

With this line, hard-coded letters are displayed right, but values from the database are displayed wrong.

当我删除该行时,我得出相反的结论:硬编码错误,数据库值正确。

When I delete the line, I get the opposite: hard-coded is wrong, database values are right.

我很困惑,因为整个内容都是UTF-8,但在PC上显示错误,但它在Notebook上有效。我想这个问题是服务器端的,或者我在导出过程中出错了。

I'm confused, because the whole content is UTF-8 but it's displayed wrong on the PC, but it works on the Notebook. I guess the problem is server-side or I did something wrong at the exporting process.

我想避免PHP函数utf8_encode()和utf8_decode()。

I'd like to avoid the PHP functions utf8_encode() and utf8_decode().

有没有人知道可能的解决方案?

Does anyone know a possible solution?

推荐答案

确保你设置您的PHP数据库连接对象的字符集。这应该在查询数据库之前完成。

Make sure you set the charset of your PHP database connection object. This should be done before querying the database.

// tells the mysqli connection to deliver UTF-8 encoded strings.
$db = new mysqli($dbHost, $dbUser, $dbPassword, $dbName);
$db->set_charset('utf8');

// tells the pdo connection to deliver UTF-8 encoded strings.
$dsn = "mysql:host=$dbHost;dbname=$dbName;charset=utf8";
$db = new PDO($dsn, $dbUser, $dbPassword);

上面的示例显示了如何设置SQLI或PDO的字符集。以这种方式准备连接对象,使您独立于数据库配置,如有必要,连接甚至可以转换返回/发送的数据。

The examples above show how to set the charset for SQLI or PDO. Preparing the connection object this way, makes you independent of the database configuration, if necessary the connection will even convert the returned/sent data.

这篇关于编码:一切都是UTF-8,但DB输出显示错误。有任何想法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 16:43