utf8重音的不一致显示

时间:2009-04-21 11:21:27

标签: php sql mysql ajax utf-8

我有一个数据库,有一个表,有一个特定字段,其中包含各种服装的描述。这些描述通常包含变音符号或类似字符。

我正在从两个不同的php字段中检索这些字段,而我没有对数据做任何事情,但它显示不一致。

在file1中,它将在下方正确显示,或添加

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

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

如果我只将上述其中一项添加到file1,则数据将无法正确显示。

在file2中,只有

才能正确显示
$con->query("SET NAMES 'utf8'"); 

已设定。

添加

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

与set names查询结合使用,或者将set names查询保留为out将导致它显示错误。

$con->set_charset("utf8");

似乎没有任何区别。

这个问题存在于Windows和Linux上的Internet Explorer和firefox上。

显示不一致的数据示例:

ED HARDY SCHUHE IM JAPAN-STYLE, GRÖßE 36

File1中:

<?php
header("Content-Type: text/html; charset=utf-8");
if (isset($_GET["pk"])) {
    $pk = $_GET["pk"];
}
$con = mysqli_connect("localhost","user","password", "database");
if (!$con) {
    echo "Can't connect to MySQL Server. Errorcode: %s\n". mysqli_connect_error();
    exit;
}
$con->query("SET NAMES 'utf8'");
$retreiveQuery = 'SELECT ARTICLE_NAME FROM AUCTIONS WHERE ARTICLE_NO = ?';
if ($getRecords = $con->prepare($retreiveQuery)) {
    $getRecords->bind_param("s", $pk);
    $getRecords->execute();
    $getRecords->bind_result($ARTICLE_NAME);
    while ($getRecords->fetch()) {
        echo "<h1>".$ARTICLE_NAME."</h1>";
    }
} else {
    print_r($con->error);
}

文件2:

<?php
header("Content-Type: text/html; charset=utf-8");
if (isset($_GET["brand"])) {
    $brand = $_GET["brand"];
}
$con = mysqli_connect("localhost","user","pass", "database");
if (!$con) {
    echo "Can't connect to MySQL Server. Errorcode: %s\n". mysqli_connect_error();
    exit;
}
$con->query("SET NAMES 'utf8'");
$brand = '%' . $brand . '%';
$rows = getRowsByArticleSearch($brand);
echo "<table border='0' width='100%'><tr>" . "\n";
foreach ($rows as $row) {
    $pk = $row['ARTICLE_NO'];
    echo '<tr id="article_' . $pk . '">' . "\n";
    echo '<td><a href="#" onclick="updateByPk(\'Layer2\', \'' . $pk . '\', \'' . $title . '\', \'' . $pg . '\')">'.$row['ARTICLE_NAME'].'</a></td>' . "\n";
    echo '</tr>' . "\n";
}
echo "</table>\n";
function getRowsByArticleSearch($searchString) {
    $con = mysqli_connect("localhost","user","pass", "db");
    $recordsQuery = "SELECT ARTICLE_NAME FROM AUCTIONS WHERE lower(ARTICLE_NAME) LIKE ? and SUBCAT='' LIMIT 0,20";
    if ($getRecords = $con->prepare($recordsQuery)) {
        $getRecords->bind_param("s", $searchString);
        $getRecords->execute();
        $getRecords->bind_result($ARTICLE_NAME);
        $rows = array();
        while ($getRecords->fetch()) {
            $row = array(
                            'ARTICLE_NAME' => $ARTICLE_NAME, 
                        );
            $rows[] = $row;
        }
        return $rows;
    } else {
        print_r($con->error);
    }
}

我有一个带有index.php的ajax应用程序,我为此设置了

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

然后我使用file2通过ajax加载一个包含内容的图层,这会错误地显示数据。数据成为一个链接,它使用file1将数据加载到不同的层中,这样可以正确显示数据。

我真的不明白不一致的来源。

编辑:

数据在file1中正确显示,设置名称和标题设置,页面显示为utf-8。

当网页信息显示字符集为ISO-8859-1时,文件2将仅正确显示数据,这意味着省略集名称并设置标题。

1 个答案:

答案 0 :(得分:0)

确保文件以utf8编码保存,而不是其他内容。 这可以通过IDE或文本编辑器完成。即在eclipse中,您可以选择编辑 - >设置编码

(我的猜测是file2.php或index.php与其他编码一起保存)

相关问题