utf 8 - PHP 和 MySQLi UTF8
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10331883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
utf 8 - PHP and MySQLi UTF8
提问by Mahdi_Nine
my table char set is utf8 and it's collation is utf8.now i have this code:
我的表字符集是 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");
}
but out put is:
但输出是:
Current character set: utf8 8 is in district ù??aù…ù??a?′?3 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.
但没有区别。
回答by Rohit Kumar Choudhary
Please replace mysql_set_charset('utf8');to $mysqli->set_charset("utf8"):-)
请替换mysql_set_charset('utf8');为$mysqli->set_charset("utf8"):-)
回答by Rinzler
or mysqli_set_charset($this->mysqli,"utf8");
mysqli_set_charset($conn,"utf8");
回答by Ed-AITpro
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 PayPal 交易远程自动创建用户帐户。Mysqli Query 创建用户和用户元数据,但该数据的值包含隐藏的格式化字符并使表单处理中调用的该数据的使用无效。非常简单的解决方案,无需更改/更改任何现有的数据库表。基本上这可以确保您的数据只是 ASCII 可打印字符,甚至可以使用 $mysqli->set_charset("utf8"); 并不是那么重要,因为您正在准备要处理的数据并输入“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 = '[email protected]';
$domain = '';
$item = 'Test';
$create_ipn->execute();
回答by wbswjc
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="Content-Type" content="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,这就是我的问题和解决方案。
回答by karrar kazuya
in your php file add the following at the top of your code
在您的 php 文件中,在代码顶部添加以下内容
header('Content-Type: text/plain; charset=utf-8');

