简体   繁体   English

立陶宛字符未正确保存到MySQL DB中

[英]Lithuanian characters not saving correctly into MySQL DB

Once again I'm having problems with saving special characters into a database. 我在将特殊字符保存到数据库时再次遇到问题。 After lots of searchs I still could not find solution so I am starting a new thread. 经过大量搜索后,我仍然找不到解决方案,因此我开始了一个新线程。

I have MySQL DB using UTF-8 character set and PHP application that reads data from XML files into DB. 我有使用UTF-8字符集的MySQL数据库和将XML文件中的数据读入数据库的PHP应用程序。 Earlier I had problems with estonian characters, which I managed to solve. 早些时候我遇到了爱沙尼亚字符的问题,我设法解决了这个问题。 For example & scaron; 例如Š (š) is in XML as html entity & eth; (š)在XML中为html实体和eth; and it is converted in PHP to & #353;. 并在PHP中将其转换为š。 Earlier in PHP script I run mysql query "SET NAMES utf8". 在PHP脚本的前面,我运行mysql查询“ SET NAMES utf8”。 š saves into DB correctly. š正确保存到数据库中。

Now I'm fighting with lithuanian characters, for example ų (& #371), which is as numeric entity, & #371;, in XML file. 现在,我正在处理立陶宛语字符,例如XML文件中的ų(&#371),它是数字实体,而ų。 I am not doing any conversion for this in PHP since I assume that when & eth; 由于我假设when&eth ;,因此我不会在PHP中对此进行任何转换。 converted to & #353; 转换为š works with scaron, shouldn't & #371; 与Scaron配合使用,不应ų save into DB as ų without PHP conversion? 保存为DB而不进行PHP转换? After save that appears in DB as question mark and if I try to use mb_convert_encoding() or html_entity_decode() result is ų. 保存后,在数据库中显示为问号,如果我尝试使用mb_convert_encoding()或html_entity_decode(),结果为ų。

Any advice? 有什么建议吗?

You simple should make sure your table has correct encoding and run SET names just after connection. 您应该简单地确保表具有正确的编码,并在连接后立即运行SET名称。

I've prepared simple test. 我准备了简单的测试。 Try to run it to make sure everything works fine. 尝试运行它以确保一切正常。

1) Create database testencoding and import the following code to it 1)创建数据库testencoding并将以下代码导入其中

CREATE TABLE IF NOT EXISTS `sample` (
`id` int(11) NOT NULL,
  `value` varchar(255) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

--
ALTER TABLE `sample`
 ADD PRIMARY KEY (`id`);

2) Create simple PHP script with following content and run it: 2)创建具有以下内容的简单PHP脚本并运行它:

<?php

header('Content-Type: text/html; charset=utf-8');
mb_internal_encoding('utf-8');
$subjectvalue='&#371; ų';



$link = mysqli_connect("localhost","root","","testencoding");
mysqli_query($link,"SET NAMES 'utf8'");
mysqli_query($link,"INSERT INTO sample(`value`) VALUES('".mysqli_real_escape_string($link,$subjectvalue)."')");


$result = mysqli_query($link, "SELECT * FROM sample");
echo "<br /><br />Data from database<br /><br />";
while ($data = mysqli_fetch_assoc($result)) {
    echo $data['id'].' '.$data['value']."<br />";
}

3) On my PC all results are as expected: 3)在我的PC上,所有结果均符合预期:

As output from PHP file I have: 作为PHP文件的输出,我有:

Data from database

1 ų ų

In phpMyadmin I have: 在phpMyadmin中,我有:

&#371; ų

So everything works fine. 因此,一切正常。 Try it and compare with my results 试试看,并与我的结果进行比较

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM