php - 通过php用mysql设置utf8

标签 php mysql character-encoding

我有以下非常简单的代码,它检索 utf8 格式的数据,例如包含来自 mysql 数据库的变音符号,它可能会或可能不会设置为 utf8。如果我使用任何一种注释掉的方法来确保返回 utf8 数据,数据将不会作为 utf8 返回,但是如果我不使用它们,数据将被显示。为什么强制使用 utf8 会否定将数据显示为 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 内容并使用 Windows-1252 的默认 HTML 字符集提供它,那么它看起来会出现乱码。确保你有这样的等价物:

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

在您的 PHP 代码中。

关于php - 通过php用mysql设置utf8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/624301/

相关文章:

php - 错误101(net::ERR_CONNECTION_RESET):在PHP中

java - 关于在 3 ignite 服务器集群上使用 sql 查询获取数据的问题

spring - 如何使用 org.springframework.web.filter.CharacterEncodingFilter 来纠正字符编码?

mysql - ActiveRecord - 非法混合排序规则

php - 用户交互时 AJAX 调用并不总是加载

php - 我如何在我的页面的 PHP 中实现一个背景 "Timer"每隔 X 次检查一些东西

java - 安卓/MySQL : how can I display data based on the department associated with the logged-in user?

c# - C#计算时间差并保存到mysql

mysql - 我不断收到这个 mysql 错误代码 #1089

c - C 中的重音/变音字符?