【问题标题】:utf 8 - PHP and MySQLi UTF8 [duplicate]utf 8 - PHP 和 MySQLi UTF8 [重复]
【发布时间】:2012-05-07 02:16:51
【问题描述】:

我的表格字符集是 utf8,它的排序规则是 utf8。现在我有了这个代码:

   $mysqli = new mysqli("localhost", "root", "", "Amoozeshgah");

            if (mysqli_connect_errno()) {
                  printf("Connect failed: %s\n", mysqli_connect_error());

            }
          if (!$mysqli->set_charset("utf8")) {
    printf("Error loading character set utf8: %s\n", $mysqli->error);
} else {
    printf("Current character set: %s\n", $mysqli->character_set_name());
}
        mysql_set_charset('utf8');
            if ($stmt = $mysqli->prepare("SELECT About_Title FROM Tbl_About WHERE About_Id=?")) {
                $city = 8;

               /* bind parameters for markers */
                $stmt->bind_param("s", $city);

              /* execute query */
                $stmt->execute();

               /* bind result variables */

                  $result = $stmt->get_result();

             /* fetch value */
            while ($myrow = $result->fetch_assoc()) {

        // use your $myrow array as you would with any other fetch
        printf("%s is in district %s\n", $city, $myrow['About_Title']);
        print("shod");

    }

但输出是:

Current character set: utf8 8 is in district نتمنتشس shod

我能做什么? 编辑: 我换了:

if (!$mysqli->set_charset("utf8")) {
        printf("Error loading character set utf8: %s\n", $mysqli->error);
    } else {
        printf("Current character set: %s\n", $mysqli->character_set_name());
    }
            mysql_set_charset('utf8');

$mysqli->set_charset("utf8")

但没有区别。

【问题讨论】:

  • 你的数据库/表设置是 utf8 的吗?

标签: php character-encoding mysqli


【解决方案1】:

请将mysql_set_charset('utf8'); 替换为$mysqli->set_charset("utf8") :-)

【讨论】:

  • 您是否在输出此输出的页眉中使用了 utf 8 编码。
  • 最好使用utf8mb4而不是utf8
【解决方案2】:
or mysqli_set_charset($this->mysqli,"utf8");
mysqli_set_charset($conn,"utf8");

【讨论】:

    【解决方案3】:

    以防万一您在代码中使用“json_encode”,例如:

    json_encode($rows);
    

    只需将其更改为:

    json_encode($rows,JSON_UNESCAPED_UNICODE);
    

    【讨论】:

      【解决方案4】:

      有趣的场景可能会帮助其他人。没有技术细节/解释,而只是朝着正确方向的推动/线索。

      场景:基于成功/验证的 IPN PayPal 交易远程自动创建用户帐户。 Mysqli Query 创建用户和 usermeta 数据,但该数据的值包含隐藏的格式字符,并使表单处理中调用的该数据的使用无效。非常简单的解决方案,无需更改/更改任何现有的数据库表。基本上,这可以确保您的数据只是 ASCII 可打印字符,甚至使用 $mysqli->set_charset("utf8");并不是那么重要,因为您正在准备要处理和输入“ASCII clean”的数据。

          $create_ipn = $mysqli->prepare("INSERT INTO xxx_somewhere (some_ipn_parameter , email, domain, item) VALUES (?, ?, ?, ?)");
          $create_ipn->bind_param("ssss", $some_ipn_parameter, $email, $domain, $item);
      
          $some_ipn_parameter = preg_replace( '/[^\x01-\x7F]/', "", 'the_actual_ipn_data' );
          $email = 'edward@ait-pro.com';
          $domain = '';
          $item = 'Test';
          $create_ipn->execute();         
      

      【讨论】:

        【解决方案5】:

        从数据库中获取数据时,不仅要正确获取数据,还需要在页面上正确显示,因此请尝试在页面标题中使用 UTF-8 编码:

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <meta charset="utf-8">

        这是我的问题和解决方案,感谢 Rohit Kumar Choudhary。

        【讨论】:

          【解决方案6】:

          在您的 php 文件中,在代码顶部添加以下内容

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

          【讨论】:

            猜你喜欢
            • 2016-08-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多