PHP 和 Mysql UTF-8 问题(特殊字符)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7073401/
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
Problem with PHP and Mysql UTF-8 (Special Character)
提问by Jean-Francois
I Have a form with one textbox called(ProductTitle)
我有一个带有一个名为(ProductTitle)的文本框的表单
if I write as example "étuit" in the textbox and click on Save, I post the data in a table called Product. The result int the database for ProductTitle is ?‰tuit. My concern is about the Special character. Instead of putting é in the database , I got that ?‰
如果我在文本框中写为示例“étuit”并单击“保存”,我会将数据发布到名为“产品”的表中。ProductTitle 的数据库结果是?‰tuit。我关心的是特殊字符。而不是把é放在数据库中,我得到了?‰
When I Load the Product Title ("étuit") from the database into a span. That show correctly.
BUT When I load it inside a Textbox to Edit the Product Title, that show ?‰tuit.
当我将产品标题(“étuit”)从数据库加载到跨度中时。那显示正确。
但是当我将它加载到文本框中以编辑产品标题时,显示 ?‰tuit。
Anybody know why.
有谁知道为什么。
I Put that in the html head
我把它放在 html 头中
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
Note :When I Click save on the form, the data is posted with jquery ajax method.
注意:当我单击表单上的保存时,数据将使用 jquery ajax 方法发布。
采纳答案by AlienWebguy
Take a look at utf8_encode()and utf8_decode(). Also take a look at multibyte string functions.
看看utf8_encode()和utf8_decode()。另请查看多字节字符串函数。
回答by CONvid19
Try seting the client encoding before using the DB.
在使用数据库之前尝试设置客户端编码。
mysql_query("SET NAMES 'utf8'");
If the above doesn't work use the utf8 encode/decode functions:
如果上述方法不起作用,请使用 utf8 编码/解码功能:
<?
$string ="étuit";
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<?
echo $string; // echo's '?tuit'
echo utf8_encode($string); // echo's 'étuit'
?>
回答by gview
Probably what is happening is that the default character set for the client is not set to UTF-8, so you're getting tranposition in one direction or the other. This is covered in a number of different ways here:
可能发生的情况是客户端的默认字符集未设置为 UTF-8,因此您在一个方向或另一个方向进行了换位。这在此处以多种不同的方式进行了介绍:
Often an initialization query of "SET NAMES utf8" just after the connection is instantiated will solve the issue going forward but make sure that what you think is stored (utf8) is actually what was stored. You might have a cleanup job if not.
通常,在连接实例化之后对“SET NAMES utf8”的初始化查询将解决未来的问题,但要确保您认为存储的内容 (utf8) 实际上是存储的内容。如果没有,您可能有清理工作。
回答by Zon
Not to bother with SET NAMES before every connection in the code, a parameter in mysql connection string can be used:
在代码中的每个连接之前不要打扰 SET NAMES,可以使用 mysql 连接字符串中的参数:
"jdbc:mysql://hostAddress:port/databaseName?characterEncoding=UTF-8"
回答by Dmitry Pavlov
This post explains how to configure and work with UTF-8 in PHP and MySQL. Hope that saves your time.
这篇文章解释了如何在 PHP 和 MySQL 中配置和使用 UTF-8。希望可以节省您的时间。
回答by lmcDevloper
These work for me:
这些对我有用:
In the HTML headers:
在 HTML 标题中:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
After the PHP connection:
PHP连接后:
$conexion = @mysql_connect($servidor, $usuario, $contrasenha);
mysql_select_db($BD, $conexion) or die(mysql_error($conexion));
mysql_query("SET NAMES 'utf8'");
回答by Samuel Corradi
I just use set_charset method when i'm using mysqli lib.
当我使用 mysqli lib 时,我只使用 set_charset 方法。
error_reporting(E_ALL);
$mysqli = new mysqli('localhost', 'login', "pass", 'database');
if ( ! $mysqli->set_charset("utf8") )
{
printf("Error loading character set utf8: %s\n", $mysqli->error);
}
回答by Fatimaezzahra Rarhibou
I also had difficulties with this, but the following always works for me ! Before manipulating your data, make sure to set the encoding as follows:
我在这方面也遇到了困难,但以下总是对我有用!在操作数据之前,请确保按如下方式设置编码:
try{
$dbh = new PDO($dsn, $user, $pass);
$dbh->query("SET NAMES 'utf8'");
print "Connected";
}
catch(PDOException $e){
print "Error!! " . $e->getMessage()."<br/>";
die();
}