V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
laoyuan
V2EX  ›  MySQL

为什么 UTF-8 编码的 MySQL 数据库碰到一些罕见字还是会报错: Incorrect string value

  •  
  •   laoyuan ·
    laoyuan · 2015-03-05 08:01:48 +08:00 · 3747 次点击
    这是一个创建于 3350 天前的主题,其中的信息可能已经有所发展或是发生改变。
    CREATE TABLE IF NOT EXISTS `testx` ( `author` varchar(10) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
    INSERT INTO `testx` (author) VALUES ('顾铮𠀡');

    Incorrect string value: '\xF0\xA0\x80\xA1' for column 'author' at row 1

    𠀡这个字是用 PHP html_entity_decode 来的:
    $author = html_entity_decode('顾铮𠀡', ENT_QUOTES, 'UTF-8');

    环境:Mac OSX 10.10 + MySQL 5.6、ubuntu 10.04 + MySQL 5.5
    laoyuan
        1
    laoyuan  
    OP
       2015-03-05 08:10:51 +08:00
    最关键的是 怎么解决呢。。
    raofeng
        2
    raofeng  
       2015-03-05 09:20:53 +08:00
    ```sql
    CREATE TABLE IF NOT EXISTS `testx` ( `author` varchar(10) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;
    INSERT INTO `testx` (author) VALUES ('顾铮𠀡');
    ```
    raofeng
        3
    raofeng  
       2015-03-05 09:21:24 +08:00   ❤️ 1
    用 utf8mb4
    laoyuan
        4
    laoyuan  
    OP
       2015-03-05 09:24:48 +08:00
    @raofeng 谢谢,我还纳闷呢,怎么冒出4个字节来,原来问题在这里
    laoyuan
        5
    laoyuan  
    OP
       2015-03-05 13:43:33 +08:00
    @raofeng 成功了,在php里连接数据库后加上 mysql_query("set names utf8mb4");
    命令行里看到这个字是问号 ?,不过echo 到页面上正常的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1315 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 23:32 · PVG 07:32 · LAX 16:32 · JFK 19:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.