简体   繁体   中英

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

I Have a form with one textbox called(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 É

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.

Anybody know why.

I Put that in the html head

<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.

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:

<?
$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() . 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. 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. 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:

"jdbc:mysql://hostAddress:port/databaseName?characterEncoding=UTF-8"

This post explains how to configure and work with UTF-8 in PHP and MySQL. Hope that saves your time.

A UTF-8 Primer for PHP and MySQL

These work for me:

In the HTML headers:

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

After the PHP connection:

$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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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