【发布时间】:2010-10-20 19:17:25
【问题描述】:
我有一个数据库,一个表,一个特定的字段,其中包含各种服装的描述。这些描述通常包含变音符号或类似字符。
我正在从两个不同的 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");
与集合名称查询结合使用,或将集合名称查询排除在外将导致其显示不正确。
$con->set_charset("utf8");
似乎在任何时候都没有区别。
此问题存在于 windows 和 linux 上的 internet explorer 和 firefox。
显示不一致的数据示例:
ED HARDY SCHUHE IM JAPAN-STYLE, GRÖßE 36
文件1:
<?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 才会正确显示数据,这意味着省略集合名称并设置标题。
【问题讨论】:
-
这个问题以前有人问过!在这里查看:stackoverflow.com/questions/346531/utf8-problem-with-mysql-5/…