Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 328
Jean-Francois Avatar asked Aug 16 '11 03:08

Jean-Francois


People also ask

Can UTF-8 handle special characters?

Since ASCII bytes do not occur when encoding non-ASCII code points into UTF-8, UTF-8 is safe to use within most programming and document languages that interpret certain ASCII characters in a special way, such as / (slash) in filenames, \ (backslash) in escape sequences, and % in printf.

Does PHP support UTF-8?

PHP | utf8_encode() FunctionThe utf8_encode() function is an inbuilt function in PHP which is used to encode an ISO-8859-1 string to UTF-8. Unicode has been developed to describe all possible characters of all languages and includes a lot of symbols with one unique number for each symbol/character.

How can I insert special characters in MySQL using PHP?

Use mysqli_real_escape_string() to Insert Special Characters Into a Database in PHP. To get user input with special characters from the form fields, we use the mysqli_real_escape_string() function. We need the following parameters: database connection and the strings we want to escape.


2 Answers

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'
?>
like image 89
Pedro Lobito Avatar answered Sep 20 '22 05:09

Pedro Lobito


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

like image 27
AlienWebguy Avatar answered Sep 19 '22 05:09

AlienWebguy