Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save Accents in MySQL Database


I'm trying to save French accents in my database, but they aren't saved like they should in the DB.
For example, a "é" is saved as "é".
I've tried to set my files to "Unicode (utf-8)", the fields in the DB are "utf8_general_ci" as well as the DB itself.
When I look at my data posted through AJAX with Firebug, I see the accent passed as "é", so it's correct.

Thanks and let me know you need more info!

like image 576
Ebpo Avatar asked Jan 31 '11 18:01

Ebpo


2 Answers

Personally I solved the same issue by adding after the MySQL connection code:

mysql_set_charset("utf8"); 

or for mysqli:

mysqli_set_charset($conn, "utf8"); 

or the mysqli OOP equivalent:

$conn->set_charset("utf8"); 

And sometimes you'll have to define the main php charset by adding this code:

mb_internal_encoding('UTF-8'); 

On the client HTML side you have to add the following header data :

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

In order to use JSON AJAX results (e.g. by using jQuery), you should define the header by adding :

header("Content-type: application/json;charset=utf8"); json_encode(      some_data ); 

This should do the trick

like image 141
Olivier Kaisin Avatar answered Sep 17 '22 13:09

Olivier Kaisin


The best bet is that your database connection is not UTF-8 encoded - it is usually ISO-8859-1 by default.

Try sending a query

SET NAMES utf8; 

after making the connection.

like image 37
Pekka Avatar answered Sep 20 '22 13:09

Pekka