php mysql character set: storing html of international content

前端 未结 4 1058
没有蜡笔的小新
没有蜡笔的小新 2020-12-06 03:54

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

4条回答
  •  -上瘾入骨i
    2020-12-06 04:24

    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.

提交回复
热议问题