1

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.

When a string is pulled from MySQL and displayed to the client the encoding is not displaying properly.

Ex:

Text should display as and does on old infrastructure:

"¿Qué planearon Sandra y Carlos en este episodio?

But it displays as:

"¿Qué planearon Sandra y Carlos en este episodio?"php

I have tried the following:

  1. Add header to client interprets all as UTF-8, no change.

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

  2. echo the encoded characters below: (they display correctly to client as: ä ö ü ß €.)

    "\xc3\xa4"."\xc3\xb6"."\xc3\xbc"."\xc3\x9f"."\xe2\x82\xac";

  3. Add encoding options to html_entity_decode for UTF-8. No Change.

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);
4

2 回答 2

1

看起来您的数据库以拉丁 1 编码而不是 UTF8 存储,快速修复应该是使用以下语法:

<?php
echo html_entity_decode($row['prompt'],ENT_COMPAT | ENT_HTML401,'ISO-8859-1');

真正的解决方法是修复您的数据库以将数据存储在 UTF8 中,并让您的所有工具链都使用 UTF8。

自 PHP 5.4 起,html_entity_decode() 将 UTF8 作为默认编码:http: //fr2.php.net/manual/en/migration54.other.php

于 2013-09-05T08:31:34.317 回答
0

Pascalc 击败了我:


在 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');
于 2013-09-05T14:02:25.120 回答