php utf8 mysql,设置utf8与mysql通过php

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?

$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 "

$ARTICLE_NAME";

}

} else {

print_r($con->error);

}

解决方案

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

in your PHP code.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值