dtrvzd1171 2015-05-23 18:34
浏览 23

如何在PHP / MySQL中获得charset? [重复]

This question already has an answer here:

I've done a very few web PHP/MySQL based web projects, and sooner or later, I always have charset issues.

I live in Spain, and we have some special characters over here: ç ñ á é í ó and ú

There are so many variables, that the charset always gets messed up:

  • MySQL database collation
  • PHP/HTML headers
  • Web browser codification settings
  • PHP settings
  • Apache settings

What I would like to have is a basic guideline on how to setup everything, so that I don't have issues with these Spanish characters.

There are three types of ways I populate my HTML output:

  • I query the MySQL database with PHP, and echo the output.
  • I write some words directly with HTML, for example

    <p>Qué rábanos pasaría mañana</p>
    
  • I read a labels.ini file with parse_ini_file($file); The label file looks something like:

    SORTING_ENTITY = Línea de negocio SORTING_PLURAL = Líneas de negocio MAIN_ENTITY = Instalación MAIN_PLURAL = Instalaciones

So when I view the website, sometimes the texts generated from MySQL are messed up, other times the direct HTML is messed up, and other times everything is okay, but the content coming from the .ini file is messed up.

Also sometimes, I use web forms, so that the users input data that is saved in MySQL. The users write for example "Pájaro" in the web form, and some incorrect chars are stored in the database like "P}jaros" or something like that.

I would like to have some guidelines, so that everything is setup in a way that whatever I write in direct HTML or .ini file is shown in the website, and whatever the users writes in the web form is stored correctly, and also displayed in the same way when later reading this data and echoing with PHP.

I don't want to be using stuff like:

&aacute;
&ntilde;
echo  utf8_encode($dat);  
</div>
  • 写回答

1条回答 默认 最新

  • dongyan7876 2015-05-23 18:44
    关注

    In HTML head element always include

    <meta charset="UTF-8"/> 
    

    To output UTF-8 charecters in PHP

    header("Content-type: 'text/html'; Charset='UTF-8');
    

    Also, remember to put headers on top of every PHP script

    For PHP CRUD(Create, Read, Update, Delete) use this code

    $conObj = new mysqli("", "", "", "");
        $conObj->query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");
    

    Try to create a class with this "query" function (call it encoder or anything you like), so whenever you make an object of this class, this function will be automatically executed and you will not have to hard code it and write under every connection instance/object.

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效