dongliao8069 2014-05-22 04:13 采纳率: 0%
浏览 20

too long

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. Earlier I had problems with estonian characters, which I managed to solve. For example & scaron; (š) is in XML as html entity & eth; and it is converted in PHP to & #353;. Earlier in PHP script I run mysql query "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. I am not doing any conversion for this in PHP since I assume that when & eth; converted to & #353; works with scaron, shouldn't & #371; save into DB as ų without PHP conversion? After save that appears in DB as question mark and if I try to use mb_convert_encoding() or html_entity_decode() result is ų.

Any advice?

  • 写回答

1条回答 默认 最新

  • douba6365 2014-05-22 05:25
    关注

    You simple should make sure your table has correct encoding and run SET names just after connection.

    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

    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:

    <?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:

    As output from PHP file I have:

    Data from database
    
    1 ų ų
    

    In phpMyadmin I have:

    &#371; ų
    

    So everything works fine. Try it and compare with my results

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?