13

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.

Jean-Francois
  • 1,899
  • 4
  • 35
  • 73

8 Answers8

12

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'
?>
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • Either or. If you set everything to UTF-8, there's no need for `utf8_encode`. Do you know what this function actually does? – deceze Aug 16 '11 at 04:36
  • deceze : it encodes a string to utf8. Nice picture you've on your profile ;) – Pedro Lobito Aug 16 '11 at 04:50
  • 2
    But the string is already encoded in UTF-8, at least it's supposed to be! While you're on my profile, please click through to the long ranty article I wrote about encodings, which goes through this in more detail. – deceze Aug 16 '11 at 04:52
  • 3
    @deceze you're taking this function way too personally and come across as a douche with all the righteous downvoting. – AlienWebguy Aug 16 '11 at 11:34
  • @Alien Yes, encodings are my current pet peeve, because too many people are spreading bad advice and half-truths about them without understanding how they really work. If you have some advice for me how I can make people understand that their knowledge about encodings is incomplete without coming across as a douche, please let me know, I'm always open to improvement. – deceze Aug 16 '11 at 11:53
  • 2
    For starters, stop being so condescending. Instead of "...do you know what this function actually does?" you could say "According to the PHP docs, the function actually...and you you can replicate that behavior like..." – AlienWebguy Aug 16 '11 at 12:12
  • @Alien Fair enough, noted. @Tuga `utf8_encode`/`utf8_decode` converts text from ISO-8859-1 to UTF-8 and vice versa and is hence the wrong function to use in this context. @Alien It would be nice if you could correct your answer, since it's currently the accepted answer and promotes the wrong solution. – deceze Aug 16 '11 at 12:25
  • liked because this helped me on my way to finding out that in my case I needed the line `$conn -> set_charset("utf8");` in my object oriented connection. – luke_mclachlan Jun 01 '21 at 16:00
10

Take a look at utf8_encode() and utf8_decode(). Also take a look at multibyte string functions.

Neuron
  • 5,141
  • 5
  • 38
  • 59
AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
  • 2
    Sorry, but unless you can **explain** what `utf8_encode` actually does and why this would be better than @gview's solution, I'll have to give you a -1. – deceze Aug 16 '11 at 04:34
  • 2
    Try saving the text "エンコード" into your UTF-8 database (which should be able to handle it, right?) using `utf8_encode`/`utf8_decode` and tell me if it's still working. – deceze Aug 16 '11 at 06:14
  • I don't need these character. I Only need French character like éè . – Jean-Francois Aug 16 '11 at 20:59
  • @Jean-Francois Maybe, but using this solution you're either storing garbage in the database or are replacing any characters that are not part of the latin-1 encoding with question marks, depending on whether you first `utf8_encode` then decode or vice versa. Maybe that doesn't bother you now because you just happen to only be using latin-1 encodable characters, but a) you're not handling UTF-8 correctly by doing so and b) it may come back to bite you one day. To handle UTF-8 correctly, set your database connection to UTF-8 as explained by @gview. – deceze Aug 16 '11 at 22:59
  • 2
    Thanks deceze, I configured my database correctly. You absolutely right. Do thing properly paid one day :) – Jean-Francois Aug 24 '11 at 14:48
9

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.

gview
  • 14,876
  • 3
  • 46
  • 51
  • The problem here, is all the answer work really good, it's hard to find who got the best answer. I Use Doctrine , and it's not necessary to apply Set Name on mysql_query. Thanks for your help too – Jean-Francois Aug 16 '11 at 16:48
  • 1
    Jean-Francois, you might want to go back and edit your question to indicate that Doctrine was involved. It would have been helpful information. Are you configuring your connection using $manager->setCharset('utf8'); It would be good to know how this was ultimately handled for future reference. – gview Aug 16 '11 at 21:14
  • As the guide in @gview's [link](http://akrabat.com/php/utf8-php-and-mysql/) suggests, setting the html form attribute "
    " solved the OP's issue for me.
    – yunusual Mar 25 '13 at 12:32
0

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"
Zon
  • 18,610
  • 7
  • 91
  • 99
0

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

Dmitry Pavlov
  • 30,789
  • 8
  • 97
  • 121
0

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'");
lmcDevloper
  • 332
  • 2
  • 8
0

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

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