简体   繁体   English

使用 PHP 从 MySQL db 显示希腊文本的问题

[英]Issue displaying Greek text from MySQL db using PHP

I have a website hosted on a GoDaddy (Shared Windows server) which I wrote some time ago using procedural PHP and a MySQL database.我有一个网站托管在 GoDaddy(共享 Windows 服务器)上,我前段时间使用程序 PHP 和 MySQL 数据库编写了该网站。 Some tables in the database hold data that is inserted by users through some pages and in some cases they insert Greek text.数据库中的某些表保存用户通过某些页面插入的数据,并且在某些情况下它们插入希腊文本。 Everything was working fine for the last 3-4 years until recently when I was informed that the website was down for some reason.在过去的 3-4 年里,一切都运行良好,直到最近我被告知该网站由于某种原因而关闭。 I contacted GoDaddy, they told me that it must be an issue with my scripts but I have not made any changes to the code for a long time now.我联系了 GoDaddy,他们告诉我这一定是我的脚本有问题,但我已经很长时间没有对代码进行任何更改了。 Anyway, apparently it was resolved the same day without me doing anything so I guess it was an issue with their servers, although they told me that nothing was changed from their side.无论如何,显然它在同一天解决了,我没有做任何事情,所以我猜这是他们服务器的问题,尽管他们告诉我他们没有任何改变。

The problem is that since then, all the pages which display Greek characters selected from the database show them like this: Άλλο Î'ίτημα instead of proper Greek text.问题是,从那时起,所有显示从数据库中选择的希腊字符的页面都将它们显示为这样: Άλλο Î'ίτημα 而不是正确的希腊文本。 I know that they were stored like that in the database (when I access it with phpMyAdmin) but I thought this was normal until now because when they were selected and displayed in the pages they were fine.我知道它们是这样存储在数据库中的(当我使用 phpMyAdmin 访问它时),但我认为直到现在这都是正常的,因为当它们被选中并显示在页面中时,它们很好。 So I don't know what has happened now and they stopped appearing correctly.所以我不知道现在发生了什么,它们不再正确显示。 By the way, Greek characters (not from the database) are displayed correctly in the pages.顺便说一句,希腊字符(不是来自数据库)在页面中正确显示。 I've searched the internet a lot about this and I have tried all possible solutions, I believe.我已经在互联网上搜索了很多关于此的内容,并且我已经尝试了所有可能的解决方案,我相信。 Some things I have checked (copied from another SO answer):我检查过的一些事情(从另一个 SO 答案复制):

  • All PHP files are saved as UTF-8 encoding (without BOM, using Notepad++)所有PHP文件保存为UTF-8编码(不带BOM,使用Notepad++)
  • MySQL database, tables and columns are all set to utf8_unicode_ci MySQL数据库,表和列都设置为utf8_unicode_ci
  • PHP's default character set is UTF-8 PHP 的默认字符集是 UTF-8

I use the following code in my header file:我在头文件中使用以下代码:

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

and then in html head:然后在 html 头中:

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

I use the following code to connect to the database in my db connection file:我使用以下代码连接到我的 db 连接文件中的数据库:

$dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );
mysqli_set_charset($dbc, 'utf8');

The server is running PHP version 5.2.17 and MySQL version is 5.0.96.服务器运行的是 PHP 版本 5.2.17,MySQL 版本是 5.0.96。

Another weird thing is that when I export the database and import it locally (using XAMPP) and using the same PHP files as on the website, Greek characters are displayed correctly on the pages.另一个奇怪的事情是,当我导出数据库并在本地导入它(使用 XAMPP)并使用与网站上相同的 PHP 文件时,希腊字符在页面上正确显示。 However, XAMPP on my laptop is running PHP version 5.4.7 and MySQL is version 5.5.27.但是,我的笔记本电脑上的 XAMPP 运行的是 PHP 5.4.7 版,而 MySQL 是 5.5.27 版。

Any suggestions would be greatly appreciated.任何建议将不胜感激。

Thanks谢谢

Are you expecting Άλλο Αίτημα ?你期待Άλλο Αίτημα吗? Do SELECT HEX(col)... -- This would be correct hex: CE86 CEBB CEBB CEBF 20... ; Do SELECT HEX(col)... -- 这将是正确的十六进制: CE86 CEBB CEBB CEBF 20... ; this would be "double encoded": C38E E280A0 C38E C2BB C38E C2BB C38E C2BF 20...这将是“双重编码”: C38E E280A0 C38E C2BB C38E C2BB C38E C2BF 20...

Read about Mojibake and Double encoding .阅读Mojibake 和 Double encoding

default_charset = "utf-8";

Using htaccess files使用 htaccess 文件

AddDefaultCharset UTF-8

test:测试:

<?php header("content-type: text/html;charset=utf-8") ?>
<!doctype>
<html>
<head>
    <meta charset="utf-8">
</head>
<body>
    <?php echo "α β γ δ ε ϝ ϛ ζ η θ ι κ λ μ ν ξ ο π ϟ ϙ ρ σ τ υ φ χ ψ ω ϡ" ?>
</body>

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

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