设置utf8与mysql通过php [英] setting utf8 with mysql through php
问题描述
我有以下非常简单的代码,它检索utf8 formatetd数据,例如包含来自mysql数据库的umlauts,可能或可能不会设置为utf8。如果我使用注释掉的方法来确保utf8数据被返回,数据将不会返回utf8,但是如果我把它们关闭,数据将被显示。为什么要强制utf8否定显示数据为utf8?
I have the following very simple code, which retrieves utf8 formatetd data, such as containing umlauts from a mysql database, which may or may not be set as utf8. If I use either of the commented out approaches to ensure that utf8 data is returned, the data will NOT be returned as utf8, however if I leave them off, the data will be displayed. Why would forcing utf8 negate displaying data as utf8?
<?php
$con = mysqli_connect("localhost", "x", "", "x");
//$con->query("SET NAMES 'utf8'");
//$con-set_charset('utf8');
$recordsQuery = "SELECT ARTICLE_NAME FROM AUCTIONS1";
if ($getRecords = $con->prepare($recordsQuery)) {
$getRecords->execute();
$getRecords->bind_result($ARTICLE_NAME);
while ($getRecords->fetch()) {
echo "<p>$ARTICLE_NAME";
}
} else {
print_r($con->error);
}
推荐答案
也许错误在于您为网页提供的字符集。如果你从数据库中获取UTF-8内容,并使用默认的HTML字符集Windows-1252,那么它会看起来乱码。确保你有这样的对等:
Perhaps the error lies in the charset you're serving the page with. If you're getting UTF-8 content out of the database and serving it with the default HTML charset of Windows-1252 then it's going to look garbled. Make sure you have the equivalent of this:
header( 'Content-Type: text/html; charset=utf-8' );
。
这篇关于设置utf8与mysql通过php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!