We recently moved from IIS 7.5 with PHP 5.3.4 to IIS 8 with PHP 5.5. We are having an issues with data pulled from MySQL. The issue was not present on the previous infrastructure.
我们最近从使用PHP 5.3.4的IIS 7.5迁移到使用PHP 5.5的IIS 8。我们遇到了从MySQL中提取数据的问题。以前的基础设施上没有这个问题。
When a string is pulled from MySQL and displayed to the client the encoding is not displaying properly.
从MySQL中提取字符串并显示给客户端时,编码未正确显示。
Ex:
Text should display as and does on old infrastructure:
文本应显示为旧基础结构:
"¿Qué planearon Sandra y Carlos en este episodio?
“¿QuéplanearonSandra y Carlos en este episodio?
But it displays as:
但它显示为:
"¿Qué planearon Sandra y Carlos en este episodio?"php
“¿Quéplanearon Sandra y Carlos en este episodio?”php
I have tried the following:
我尝试过以下方法:
-
Add header to client interprets all as UTF-8, no change.
向客户端添加标头将全部解释为UTF-8,无变化。
header('content-type: text/html; charset=utf-8');
header('content-type:text / html; charset = utf-8');
-
echo the encoded characters below: (they display correctly to client as: ä ö ü ß €.)
回显下面的编码字符:(它们正确显示给客户:äöü߀。)
"\xc3\xa4"."\xc3\xb6"."\xc3\xbc"."\xc3\x9f"."\xe2\x82\xac";
-
Add encoding options to html_entity_decode for UTF-8. No Change.
为UTF-8的html_entity_decode添加编码选项。没变。
Any ideas? Code blow.
有任何想法吗?代号打击。
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
// CONNECT TO THE DATABASE
$DB_NAME = 'removed';
$DB_HOST = 'removed';
$DB_USER = 'removed';
$DB_PASS = 'removed';
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
// A QUICK QUERY ON A FAKE USER TABLE
$query = "SELECT * FROM `tablename` WHERE `item`='1234'";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
// GOING THROUGH THE DATA
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo html_entity_decode($row['prompt']);
}
}
else {
echo 'NO RESULTS';
}
// CLOSE CONNECTION
mysqli_close($mysqli);
2 个解决方案
#1
1
Looks like your database is stored in Latin 1 encoding and not in UTF8, the quick fix should be to use this syntax:
看起来您的数据库以Latin 1编码存储而不是UTF8,快速修复应该使用以下语法:
<?php
echo html_entity_decode($row['prompt'],ENT_COMPAT | ENT_HTML401,'ISO-8859-1');
The real fix would be to fix your database to store data in UTF8 and have all of your toolchain in UTF8.
真正的解决方法是修复数据库以UTF8存储数据并使用UTF8中的所有工具链。
html_entity_decode() has UTF8 as default encoding since PHP 5.4: http://fr2.php.net/manual/en/migration54.other.php
自PHP 5.4起,html_entity_decode()将UTF8作为默认编码:http://fr2.php.net/manual/en/migration54.other.php
#2
0
Pascalc beat me to it:
帕斯卡尔打败了我:
It appears the default encoding for HTML_entities_decode changed after php 5.4 ISO-8559-1 to UTF-8. So content that was put into MySQL with old PHP was done so as ISO-8859-1. The below did the trick for me.
在php 5.4 ISO-8559-1到UTF-8之后,HTML_entities_decode的默认编码似乎已更改。所以用旧的PHP放入MySQL的内容就像ISO-8859-1一样。以下为我做了诀窍。
html_entity_decode($row['prompt'], ENT_QUOTES, 'ISO-8859-1');
#1
1
Looks like your database is stored in Latin 1 encoding and not in UTF8, the quick fix should be to use this syntax:
看起来您的数据库以Latin 1编码存储而不是UTF8,快速修复应该使用以下语法:
<?php
echo html_entity_decode($row['prompt'],ENT_COMPAT | ENT_HTML401,'ISO-8859-1');
The real fix would be to fix your database to store data in UTF8 and have all of your toolchain in UTF8.
真正的解决方法是修复数据库以UTF8存储数据并使用UTF8中的所有工具链。
html_entity_decode() has UTF8 as default encoding since PHP 5.4: http://fr2.php.net/manual/en/migration54.other.php
自PHP 5.4起,html_entity_decode()将UTF8作为默认编码:http://fr2.php.net/manual/en/migration54.other.php
#2
0
Pascalc beat me to it:
帕斯卡尔打败了我:
It appears the default encoding for HTML_entities_decode changed after php 5.4 ISO-8559-1 to UTF-8. So content that was put into MySQL with old PHP was done so as ISO-8859-1. The below did the trick for me.
在php 5.4 ISO-8559-1到UTF-8之后,HTML_entities_decode的默认编码似乎已更改。所以用旧的PHP放入MySQL的内容就像ISO-8859-1一样。以下为我做了诀窍。
html_entity_decode($row['prompt'], ENT_QUOTES, 'ISO-8859-1');