mysql如何存储emoji表情

木木老贼 互联网营销达人~

在mysql中如何存储emoji表情,如果mysql是utf-8编码,存入表情符出错我们如何解决,下面我们来介绍一下.

utf8的数据库,存入表情符,会出错,代码如下:

Incorrect string value: '\xF0\x9F\x98\x84\xF0\x9F...' for column 'content'

错误的解决办法:

  1. 4byteUnicodecharactersaren'tyetwidelyused,sonoteveryapplicationouttherefullysupportsthem.MySQL5.5worksfinewith4bytecharacterswhenproperlyconfigured–checkifyourothercomponentscanworkwiththemaswell.
  2. Here'safewotherthingstocheckout:
  3. Makesureallyourtables'defaultcharactersetsandtextfieldsareconvertedtoutf8mb4,inadditiontosettingtheclient&servercharactersets,e.g.ALTERTABLEmytablecharset=utf8mb4,MODIFYCOLUMNtextfield1VARCHAR(255)CHARACTERSETutf8mb4,MODIFYCOLUMNtextfield2VARCHAR(255)CHARACTERSETutf8mb4;andsoon.
  4. Ifyourdataisalreadyintheutf8characterset,itshouldconverttoutf8mb4inplacewithoutanyproblems.Asalways,backupyourdatabeforetrying!
  5. Alsomakesureyourapplayersetsitsdatabaseconnections'charactersettoutf8mb4.Double-checkthisisactuallyhappening–ifyou'rerunninganolderversionofyourchosenframework'smysqlclientlibrary,itmaynothavebeencompiledwithutf8mb4supportanditwon'tsetthecharsetproperly.Ifnot,youmayhavetoupdateitorcompileityourself.//开源软件:phpfensi.com
  6. Whenviewingyourdatathroughthemysqlclient,makesureyou'reonamachinethatcandisplayemoji,andrunaSETNAMESutf8mb4beforerunninganyqueries.
  7. Onceeverylevelofyourapplicationcansupportthenewcharacters,youshouldbeabletousethemwithoutanycorruption.

总结就是:表结构改为支持4字节的unicode,数据库连接也用这个字符集哦,证明是可行的,如果别的地方不支持,可以考虑去掉这些字符:

  1. Since4-byteUTF-8sequencesalwaysstartwiththebytes0xF0-0xF7,thefollowingshouldwork:
  2. $str=preg_replace('/[\xF0-\xF7].../s','',$str);
  3. Alternatively,youcouldusepreg_replaceinUTF-8modebutthiswillprobablybeslower:
  4. $str=preg_replace('/[\x{10000}-\x{10FFFF}]/u','',$str);
  5. Thisworksbecause4-byteUTF-8sequencesareusedforcodepointsinthesupplementaryUnicodeplanesstartingfrom0x10000.

相关广告
  • mysql如何存储emoji表情 mysql如何存储emoji表情 mysql如何存储emoji表情
相关阅读

mysql如何存储emoji表情

2019/10/10 17:32:40 | 谷歌SEO算法 | 云计算