26

我的表字符集是 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")

但没有区别。

4

6 回答 6

57

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

于 2012-04-26T11:03:53.753 回答
25
or mysqli_set_charset($this->mysqli,"utf8");
mysqli_set_charset($conn,"utf8");
于 2014-02-12T12:33:49.180 回答
1

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

json_encode($rows);

只需将其更改为:

json_encode($rows,JSON_UNESCAPED_UNICODE);
于 2020-07-08T08:42:39.407 回答
0

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

场景:基于成功/验证的 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();         
于 2015-10-20T22:12:36.820 回答
0

When fetching data from database, you do not only get the data correctly, but also need to show it on pages correctly, so try using UTF-8 encoding in your page header:

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

<meta charset="utf-8">

That's my problem and solution, thanks to Rohit Kumar Choudhary.

于 2016-10-21T03:31:14.827 回答
-1

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

header('Content-Type: text/plain; charset=utf-8');
于 2018-07-02T08:41:35.057 回答