Mysql显示汉字
我长期以来一直使用php + mysql(phpmyadmin)构建中文内容(utf-8)的网站。
输入表单,并从db生成输出php时,中文单词显示良好;但是当我查看数据库时,虽然有时它们是正常的汉字,但它们不是(变成奇怪的字符串),这让我注意到,mysql处理和输入数据的方式并不总是utf-8。
网上有高手提到,mysql是通过latin1来记录输入数据的;尽管如此,我注意到phpmyadmin中现有的字符集是utf-8...
是否有任何可靠的方法来检测phpmyadmin表格单元格中出现的中文字符的编码格式?
另外,除了在页面标题中提到之外,是否有任何方法可以确保输入到数据库的数据是 utf-8 而不是其他数据?
谢谢。
I have been using php + mysql (phpmyadmin) to construct websites with Chinese contents (utf-8) for a long time.
When inputting forms, and also generate output php from db, the Chinese Words display well; but when I look at the database, although sometimes they are normal chinese characters, but something they are not (become strange strings), that made me notice that, the way that mysql handle and input data is not always utf-8.
Some experts on web mentioned, mysql were used to record the input data by latin1; nevertheless, I note that the existing charset in phpmyadmin is utf-8...
Will there be any solid way to detect the encoding format of chinese characters appeared in a phpmyadmin table cell?
Also, apart from mentioning at header of the page, will there be any method so that I can make sure the data entered to the db is utf-8 but not others?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
人们在这方面遇到的最大问题是,他们在连接数据库时没有告诉MySQL他们正在发送/期望UTF-8编码的数据,因此MySQL认为它应该处理latin1编码的数据并进行相应的转换。连接到数据库后发出命令
SET NAMES utf8
或使用mysql_set_charset
。The biggest problem that people encounter in this regard is that they don't tell MySQL that they're sending/expecting UTF-8 encoded data when connecting to the database, so MySQL thinks it's supposed to handle latin1 encoded data and converts it accordingly. Issue the command
SET NAMES utf8
after connecting to the db or usemysql_set_charset
.就我而言,这只是因为
htmlentities();
解决方案是将echo htmlentities($email_db);
更改为echo htmlentities($email_db, ENT_COMPAT, 'UTF- 8');
in my case, it just because
htmlentities();
Solution is changeecho htmlentities($email_db);
toecho htmlentities($email_db, ENT_COMPAT, 'UTF-8');