UTF-8 - PHP和库MySQLi UTF8(utf 8 - PHP and MySQLi

2019-06-23 23:49发布

我的表字符集是UTF8,它的排序是utf8.now我有这样的代码:

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

但没有什么区别。

Answer 1:

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



Answer 2:

or mysqli_set_charset($this->mysqli,"utf8");
mysqli_set_charset($conn,"utf8");


Answer 3:

有趣的场景,可以帮助别人了。 没有技术细节/解释,而不是仅仅在正确的方向推/线索。

场景:基于成功/通过IPN PayPal交易的用户帐户的远程自动创建。 mysqli的查询创建用户和usermeta数据,但这些数据的价值包含隐藏格式字符和无效数据调用表单处理的使用。 很简单的解决方案,而不改变/改变任何现有的数据库表。 基本上,这可以确保您的数据仅仅是ASCII可打印字符,甚至使用$ mysqli-> set_charset(“UTF-8”); 是不是真的那么重要,因为你是准备你的数据进行处理并输入“ASCII干净”。

    $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();         


Answer 4:

当获取从数据库中的数据,你不只是正确地获取数据,还需要正确显示它的网页,所以尝试在网页标题使用UTF-8编码:

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

<meta charset="utf-8">

这是我的问题和解决方案,这要归功于罗希特·库马尔·乔杜里。



Answer 5:

在你的PHP文件中添加下面的代码的顶部

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


文章来源: utf 8 - PHP and MySQLi UTF8