【问题标题】:Problems with charset in PHP/MySQLPHP/MySQL 中的字符集问题
【发布时间】:2014-03-24 21:39:54
【问题描述】:

我在 PHP/MySQL 中遇到了一些特殊字符的问题(我不知道是哪一个)。 然后,我使用 php 将数据存储在数据库中。我确保在存储数据之前添加此代码:

  header('Content-type:text/html; charset=utf-8');
  $link=mysqli_connect("host","user","","db") or die(mysqli_error($link));
  mysqli_query($link,"SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8';SET NAMES utf8");

我还确保 Apache 使用正确的字符集:

AddDefaultCharset utf-8

此外,我还尝试在每个 html 页面上添加 meta 标签:

<meta http-equiv="content-type" content="text/html" charset="UTF-8">

此外,我以UTF-8 格式保存了我所有的phphtml 文件。 我注意到,如果我在页面中写一个像ò 这样的字符并显示该页面,它就会正确显示。如果我从包含ò 的数据库中获取字符串查询,它不会正确显示,但如果我将代码更改为:

header('Content-type:text/html; charset=iso-8859-1');

从数据库中检索到的查询正确显示,正常写入不正确。 那么,问题出在mysql字符集上吗?如果我按照之前的说明设置会怎么样?

【问题讨论】:

    标签: mysql utf-8 character-encoding special-characters iso-8859-1


    【解决方案1】:

    尝试将存储此日期的列也更改为 utf8

    这里有一些你可以使用的代码。

    更改字符集/排序规则 (database):

    ALTER DATABASE db_name 默认字符集 utf8;

    更改字符集/排序规则 (table):

    ALTER TABLE tbl_name 默认字符集 utf8;

    更改字符集/排序规则 (columns):

    ALTER TABLE tbl_name 转换为字符集 utf8;

    【讨论】:

    • 我尝试了这些说明,现在可以使用了。谢谢 echo_Me
    【解决方案2】:

    尝试使用http://fi2.php.net/mysqli_set_charset 函数代替您现在使用的mysqli_query()。

    【讨论】:

    • 我也试过mysqli_set_charset函数,但没有。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-20
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 2011-03-10
    • 2018-11-29
    相关资源
    最近更新 更多