简体   繁体   English

PHP 和 Mysql UTF-8 问题(特殊字符)

[英]Problem with PHP and Mysql UTF-8 (Special Character)

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.如果我在文本框中写入示例“Étuit”并单击“保存”,我会将数据发布到名为 Product 的表中。 The result int the database for ProductTitle is Étuit. ProductTitle 数据库中的结果是 Étuit。 My concern is about the Special character.我担心的是特殊字符。 Instead of putting É in the database, I got that É我没有将 É 放入数据库,而是得到了 É

When I Load the Product Title ("Étuit") from the database into a span.当我将数据库中的产品标题(“Étuit”)加载到跨度中时。 That show correctly.正确显示。
BUT When I load it inside a Textbox to Edit the Product Title, that show É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 方法发布。

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'
?>

Take a look at utf8_encode() and utf8_decode() .看看utf8_encode()utf8_decode() Also take a look at multibyte string functions .还要看看多字节字符串函数

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.可能发生的情况是客户端的默认字符集未设置为 UTF-8,因此您在一个方向或另一个方向上进行换位。 This is covered in a number of different ways here:此处以多种不同方式对此进行了介绍:

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.通常,在连接实例化之后对“SET NAMES utf8”的初始化查询将解决未来的问题,但请确保您认为存储的内容(utf8)实际上是存储的内容。 You might have a cleanup job if not.如果没有,你可能有一个清理工作。

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"

This post explains how to configure and work with UTF-8 in PHP and MySQL.这篇文章解释了如何在 PHP 和 MySQL 中配置和使用 UTF-8。 Hope that saves your time.希望能节省您的时间。

A UTF-8 Primer for PHP and MySQL用于 PHP 和 MySQL 的 UTF-8 底漆

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM