本文介绍了为什么我需要mb_convert_encoding才能显示正确的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站有charset = UTF-8,而且mysql DATABASE也设置为UTF-8编码。我的问题是,当我从mysql中检索文本时到网站,我需要使用php函数mb_convert_encoding(@ db_field,'utf-8','iso-8859-1')来正确显示文本。它看起来像那个系统认为mysql数据库是在iso-8859-1。

解决方案

连接到数据库后运行此权限:

  mysqli_query(SET NAMES utf8); / *或mysql_query * / 

仅将表的排序规则设置为UTF-8是不够的,您还需要指定(使用此查询)PHP和MySQL服务器之间在通信期间应使用的编码。
更多详细信息,请参见:



确保UTF-8兼容性到处都是棘手的;我的UTF-8清单如下:


  • 确保每个源文件保存为UTF-8

  • 确保所有页面上都存在< meta charset =UTF-8/> 确保每个数据库表设置为使用UTF-8连接到数据库后运行 SET NAMES utf8
  • li>在脚本开头调用 mb_internal_encoding(UTF-8),并尽可能使用 mb _ 字符串函数


my website have charset=UTF-8, and mysql DATABASE is also set to be UTF-8 encoding.

my problem is that when I retrieve text from mysql to the website, i need to use php function mb_convert_encoding(@db_field,'utf-8','iso-8859-1') to show the text properly. it looks like that system think mysql DB is in iso-8859-1.

解决方案

Run this right after you connect to the database:

mysqli_query("SET NAMES utf8"); /* or mysql_query */

It is not enough to set the collation of the table to UTF-8, you also need to specify (with this query) the encoding that should be used during the communication between PHP and the MySQL server.
More details here: SET NAMES utf8 in MySQL?

Ensuring UTF-8 compatibility everywhere is tricky; my UTF-8 checklist is as follows:

  • Make sure every source file is saved as UTF-8
  • Ensure <meta charset="UTF-8" /> is present on all pages
  • Make sure every database table is set to use UTF-8
  • Run SET NAMES utf8 after connecting to the database
  • Call mb_internal_encoding("UTF-8") at the beginning of the script and use mb_ string functions where possible

这篇关于为什么我需要mb_convert_encoding才能显示正确的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 06:01