dton37910 2014-04-28 10:23
浏览 44
已采纳

MySQL无法与PHP的元音突变进行交互

I'm pretty desperate on this one.

Thing is I'm from Germany and we got vowel mutations (ä,ü,ö. etc). I got a location database and want to make some queries to it. Hence it all worked fine and when I was trying to find "München" (you might know it as Munich) it broke. In general I'm using the TableGateways provided by ZF2, but they are not the problem (check the list below).

Basically it always screws up on vowel mutations. What I tried and checked:

PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF8" (no effect)

doublechecking the database collation ( utf8 general_ci)

checked my file_encoding (utf8)

playing arround with iconv and utf8_de/encode (no effect)

Logging the queries which flew by

 140428 11:59:49      394 Query SET PROFILING=1
  394 Query SHOW STATUS
  394 Query SHOW STATUS
  394 Query SELECT `location`.* FROM `location` WHERE `city` LIKE 'münc%'
  394 Query SHOW STATUS

Trying the query on navicat and by terminal (worked)

and writing a quick and dirty mysql_connect(); mysql_query()- Test which also failed.

Edited the mysql config to use utf over latin1 as default charset.

And now I don't have the slightest clue what else I can do to fix it.

  • 写回答

1条回答 默认 最新

  • douwen7516 2014-04-28 10:29
    关注

    Try to use utf8_unicode_ci collation instead of utf8_general_ci. General_ci removes vowels and other special characters and translate all of them to "A" and "U", for example:

    ÀÁÅåāă = A
    ü = U
    

    utf8_unicode_ci slower, but works with this characters as you expect.

    UPDATE:

    sql:

    CREATE TABLE `location` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `city` varchar(120) COLLATE utf8_unicode_ci NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
    
    INSERT INTO `location` VALUES (1,'München');
    

    script:

    $dbh = new PDO('mysql:host=localhost;dbname=test', 'root', 'xxxxx',array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $q = $dbh->prepare("SELECT * FROM `location` WHERE `city` LIKE 'münc%'");
    $q->execute();
    print_r($q->fetch());
    

    And executing:

    $ php catalog_mdmtointra.php                                                                                                                                                                                                            
    Array                                                                                                                                                                                                                                                                          
    (                                                                                                                                                                                                                                                                              
        [id] => 1                                                                                                                                                                                                                                                                  
        [0] => 1                                                                                                                                                                                                                                                                   
        [city] => München                                                                                                                                                                                                                                                          
        [1] => München                                                                                                                                                                                                                                                             
    )            
    

    UPDATE2:

    My versions:

    ii  percona-server-common-5.5            5.5.33-rel31.1-566.wheezy          i386         Percona Server database common files (e.g. /etc/mysql/my.cnf)
    ii  php5-mysqlnd                         5.4.4-14+deb7u2                    i386         MySQL module for php5 (Native Driver)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示