drlma06060 2009-03-25 11:46
浏览 27
已采纳

php mysql字符集:存储国际内容的html

i'm completely confused by what i've read about character sets. I'm developing an interface to store french text formatted in html inside a mysql database.

What i understood was that the safe way to have all french special characters displayed properly would be to store them as utf8. so i've created a mysql database with utf8 specified for the database and each table. I can see through phpmyadmin that the characters are stored exactly the way it is supposed to. But outputting these characters via php gives me erratic results: accented characters are replaced by meaningless characters. Why is that ?

do i have to utf8_encode or utf8_decode them? note: the html page character encodign is set to utf8.

more generally, what is the safe way to store this data? Should i combine htmlentities, addslashes, and utf8_encode when saving, and stripslashes,html_entity_decode and utf8_decode when i output?

  • 写回答

4条回答 默认 最新

  • dongxibeng5324 2009-03-25 11:52
    关注

    MySQL performs character set conversions on the fly to something called the connection charset. You can specify this charset using the sql statement

    SET NAMES utf8
    

    or use a specific API function such as mysql_set_charset():

    mysql_set_charset("utf8", $conn);
    

    If this is done correctly there's no need to use functions such as utf8_encode() and utf8_decode().

    You also have to make sure that the browser uses the same encoding. This is usually done using a simple header:

    header('Content-type: text/html;charset=utf-8');
    

    (Note that the charset is called utf-8 in the browser but utf8 in MySQL.)

    In most cases the connection charset and web charset are the only things that you need to keep track of, so if it still doesn't work there's probably something else your doing wrong. Try experimenting with it a bit, it usually takes a while to fully understand.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址