UTF8 - PHP和MySQLi UTF8。

时间:2022-09-26 08:51:43

my table char set is utf8 and it's collation is utf8.now i have this code:

我的表char设置是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");

    }

but out put is:

但将是:

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

what can i do? Edit: i replaced:

我能做什么?编辑:我所取代:

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');

with

$mysqli->set_charset("utf8")

but no difference.

但没有区别。

4 个解决方案

#1


42  

Please replace mysql_set_charset('utf8'); to $mysqli->set_charset("utf8") :-)

请替换mysql_set_charset(use utf8);美元mysqli - > set_charset(use utf8):-)

#2


15  

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

#3


0  

Interesting scenario that may help someone else out. No technical details/explanation and instead just a push/clue in the right direction.

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

The scenario: remote automated creation of a User Account based on a successful/VERIFIED IPN PayPal Transaction. Mysqli Query creates user and usermeta data, but the value of that data contains hidden formatting characters and invalidates usage of that data called in Form processing. Very simple solution without changing/altering any existing DB Tables. Basically this ensures that your data is only ASCII printable characters and even using $mysqli->set_charset("utf8"); is not really that important since you are preparing your data to be processed and inputted "ASCII clean".

场景:远程自动创建一个基于成功/验证的IPN贝宝交易的用户帐户。Mysqli查询创建用户和usermeta数据,但是该数据的值包含隐藏的格式化字符,并使表单处理中调用的数据无效。非常简单的解决方案,无需更改/修改现有的DB表。这基本上确保了您的数据只是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();         

#4


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:

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

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

< meta http-equiv = " - type”内容= " text / html;charset = utf - 8 " >

<meta charset="utf-8">

< meta charset = " utf - 8 " >

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

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

#1


42  

Please replace mysql_set_charset('utf8'); to $mysqli->set_charset("utf8") :-)

请替换mysql_set_charset(use utf8);美元mysqli - > set_charset(use utf8):-)

#2


15  

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

#3


0  

Interesting scenario that may help someone else out. No technical details/explanation and instead just a push/clue in the right direction.

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

The scenario: remote automated creation of a User Account based on a successful/VERIFIED IPN PayPal Transaction. Mysqli Query creates user and usermeta data, but the value of that data contains hidden formatting characters and invalidates usage of that data called in Form processing. Very simple solution without changing/altering any existing DB Tables. Basically this ensures that your data is only ASCII printable characters and even using $mysqli->set_charset("utf8"); is not really that important since you are preparing your data to be processed and inputted "ASCII clean".

场景:远程自动创建一个基于成功/验证的IPN贝宝交易的用户帐户。Mysqli查询创建用户和usermeta数据,但是该数据的值包含隐藏的格式化字符,并使表单处理中调用的数据无效。非常简单的解决方案,无需更改/修改现有的DB表。这基本上确保了您的数据只是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();         

#4


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:

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

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

< meta http-equiv = " - type”内容= " text / html;charset = utf - 8 " >

<meta charset="utf-8">

< meta charset = " utf - 8 " >

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

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