问题描述
我目前在将特殊字符(®&©)保存到mySQL数据库时遇到问题.
I am currently having an issue saving special characters (® & ©) to a mySQL database.
在本地堆栈(对我的开发机而言是本地的)上,该操作平稳运行,并且前端输入的内容被保存为预期的内容.
On a local stack (local to my development machine) the operation runs smoothly and what is entered in the front-end gets saved as what is expected.
在集中式服务器上,使用相同的前端代码保存到DB时,该字符与其他字符一起被另存为®.在前端查看记录时,这不是问题,因为它可以反转记录并正确显示.
With the same front-end code on the centralised server when saving to the DB the character is proceeded with other characters it is saved as ®.This isn't an issue when viewing the record on the front-end as it reverses what it does and displays correctly.
该问题来自另一个使用相同数据库且不做任何更改的单独系统,因此显示为®而不是简单的®.
The issue comes from another separate system which utilises the same database and doesn't do any alteration so appears as the ® instead of simply ®.
我感到困惑的原因是,它似乎可以在我的本地堆栈上正常工作,但不能在集中式服务器上工作.
The reason for my confusion is that it seems to work as required on my local stack but not on the centralised server.
与我的本地服务器相比,我正在寻找有关在服务器配置上可能导致此问题的原因的想法.
I am looking for ideas as to what to look for which might be causing this on the servers configuration compared to my local.
先谢谢了标记
推荐答案
@Pranav Hosangadi(感谢)介绍了三个方面,以检查编码的一致性.以下解决方案对此有所补充.对于某些情况(在我能够重现并找到您的问题的解决方案时并不需要此答案),某些情况下,这个答案并不能解决问题,@ Soaice Mircea的答案(也要感谢)可能也是值得考虑的(变体) . @Pranav的思路对于此问题似乎是成功的,因为它涉及在各处而不是特定位置使用一个字符集的一致性.
@Pranav Hosangadi (thanks) covers three areas to check for consistency of encoding. The following solution adds to that. It may also be worth considering (a variation of) @Soaice Mircea's answer (thanks also) for some scenarios whereby this answer doesn't fix the problem although this wasn't required when I was able to reproduce and find a solution to your problem. @Pranav's line of thinking seems to be successful for this problem as it's about consistency of using one character set everywhere rather than a particular one.
要做的五件事:
-
确保数据库字符集和表始终使用相同的字符集,例如在phpmyadmin中进行检查,请注意并在下面使用该字符集
ensure database charset and tables use same charset throughout, inspect this in phpmyadmin for example, note and this charset for use below
对数据库字符集使用php header()函数,例如:
use php header() function with database charset e.g.:
header('Content-Type: text/html; charset=latin1_swedish_ci');
在HTML标头中插入元标记,例如:
insert meta tag in html header e.g.:
<meta http-equiv="content-type"
content="text/html;charset=latin1_swedish_ci">
在表单标签中添加字符集
add charset-accept in form tag
<form action=\"testsubmit.php\" method=\"post\" accept-charset=\"latin1_swedish_ci\">
设置mysql连接的字符集,例如:
set charset of mysql connection e.g.:
$con = mysql_connect("localhost","test","test");
mysql_set_charset ( "latin1_swedish_ci", $con );
这篇关于将特殊字符保存到MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!