Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

php - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...