php mysql character set: storing html of international content

前端 未结 4 1064
没有蜡笔的小新
没有蜡笔的小新 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条回答
  •  情歌与酒
    2020-12-06 04:33

    It is useful to consider the PHP-generated front end and the MySQL backend separate components. MySQL should not have to worry about display logic, nor should PHP assume that the backend does any sort of preprocessing on the data.

    My advice would be to store the data in plain characters using utf8 encoding, and escape any dangerous characters with MySQLs methods. PHP then reads the utf8 encoded data from database, processes them (with htmlentities(), most often), and displays it via whichever template you choose to use.

    Emil H. correctly suggested using

     SET NAMES utf8
    

    which should be the first thing you call after making a MySQL connection. This makes the MySQL treat all input and output as utf8.

    Note that if you have to use utf8_encode or utf8_decode functions, you are not setting the html character encoding correctly. It is easiest to require that every component of your system uses utf8, since that way you should never have to do manual encoding/decoding, which can cause hard to track issues later on.

提交回复
热议问题