[英]Character encoding (i/î/ï) in PHP/MySQL
I'm having troubles with accents on the character i (ï/î) to be put into a MySQL database using PHP.我在使用 PHP 将字符 i (ï/î) 上的重音符号放入 MySQL 数据库时遇到了问题。 I've 2 names, Myîa and Myïa, and after the inserts I only have Myïa in my database.
我有 2 个名字,Myîa 和 Myïa,在插入之后,我的数据库中只有 Myïa。
The collation of the table is set to utf8_unicode_ci and the field itself is also utf8_unicode_ci.表的排序规则设置为 utf8_unicode_ci,字段本身也是 utf8_unicode_ci。 I've checked the
DEFAULT_CHARACTER_SET_NAME
in the SCHEMATA table
which is set to utf8 for the database
.我已经检查了
SCHEMATA table
的DEFAULT_CHARACTER_SET_NAME
,该SCHEMATA table
set to utf8 for the database
。
database.php:数据库.php:
$dbs_cnt = mysqli_connect($dbs_hst, $dbs_usr, $dbs_psw, $dbs_nme);
if (!$dbs_cnt) { die(mysqli_connect_error()); }
if (!mysqli_set_charset($dbs_cnt, "utf8")) { echo mysqli_error($dbs_cnt); }
script.php脚本文件
header('Content-Type: text/html; charset=utf-8');
include 'database.php';
// irrelevant code
$SQL = "
INSERT INTO " . $dbs_tbl . " (`Character`)
VALUES ('" . $CHR_NME . "')
";
if (!mysqli_query($dbs_cnt, $SQL)) { echo mysqli_error($dbs_cnt); }
Everything seemed to be working fine until I came across these names recently.一切似乎都运行良好,直到我最近遇到这些名字。
When trying to manually enter them in the database when one record is already present, I am getting the following errors:当已经存在一条记录时尝试在数据库中手动输入它们时,我收到以下错误:
#1062 - Duplicate entry 'Myîa' for key 'PRIMARY'
#1062 - Duplicate entry 'Myïa' for key 'PRIMARY'
I've been playing around with the collation (as it was latin1_swedish_ci), but that doesn't seem to help.我一直在玩排序规则(因为它是 latin1_swedish_ci),但这似乎没有帮助。 After checking similar problems I've added the header aswell, but with no result either.
在检查了类似的问题后,我也添加了标题,但也没有结果。
What am I missing?我错过了什么?
I can not comment , so I send this link as an answer.我无法发表评论,所以我发送此链接作为答案。 A similar problem of mine is solved with the link below.
通过以下链接解决了我的类似问题。 Change MySQL default character set to UTF-8 in my.cnf?
在 my.cnf 中将 MySQL 默认字符集更改为 UTF-8?
sudo gedit /etc/mysql/my.cnf
[client]
default-character-set = utf8
[mysql]
default-character-set = utf8
[mysqld]
skip-character-set-client-handshake
collation-server = utf8_unicode_ci
init-connect = 'SET NAMES utf8'
character-set-server = utf8
I restarted the system and it worked (maybe a restart of mysql can be sufficent).我重新启动了系统并且它工作了(也许重新启动 mysql 就足够了)。
Set the collation to "utf8_bin".将排序规则设置为“utf8_bin”。 The glyphs i / ï / î are identical in "utf8_unicode_ci".
“utf8_unicode_ci”中的字形 i / ï / î 是相同的。 (Or choose another collation that treats those letters as distinct.)
(或者选择另一种将这些字母视为不同的排序规则。)
Rigth after connecting to mysql, execute the following query: SET NAMES utf8
.连接到 mysql 后,执行以下查询:
SET NAMES utf8
。
This is requried if you want to send UTF8 data to the server.如果要将 UTF8 数据发送到服务器,则需要这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.