Home>Article>Backend Development> How to solve php emoji mysql error problem
##The operating environment of this article: Windows7 system, PHP7.1 version, Dell G3 computerphp emoji mysql error is because the database does not support emoticons. The solution is: 1. Change the character set to utfmb4; 2. Escape the emoji expression and convert it back when calling.
How to solve the php emoji mysql error problem?
function en_emoji($str){ $en_str=''; $length=mb_strlen($str,'utf-8'); for($i=0;$i=4){ $en_str.=''.rawurlencode($tmp_str).''; }else{ $en_str.=$tmp_str; } } return $en_str; } //反转义为emoji function de_emoji($str){ $de_str=preg_replace_callback('|(.*?)|',function($matches){ return rawurldecode($matches[1]); },$str); return $de_str; }The usage method is also very simple. Escape the data when storing it in the database, and then reverse it when reading and displaying it. The result after en_emoji($str) is: Use de_emoji($str) when reading and displaying, and that’s it. Comparison of two solutions: Method 1: Modify the character set, not only the database, but also the program code. But how to put it, the database may be upgraded to utf8mb4 in the future. Method 2: Using the function method, you only need to modify the program and do not need to touch the database. Two functions are implemented. But compared to the content in the database, it feels like some garbled characters. And when I was writing this article, I found that because en_emoji was used when storing data, all expressions became escaped codes. When I used anti-escape de_emoji when displaying the article content, this time Later I discovered that the escaped content that I originally wanted to express in my article had also been de-escaped. You can see that I can only use a picture to replace the performance of my results above. Conclusion: Use less emojis. It’s troublesome anyway. Of course, if you make the database yourself and write the program yourself, it’s more perfect to set up utf8mb4 using method one. But to be honest, I only used the escape function when writing this article, and stopped it after writing to avoid unnecessary trouble. Recommended: "
PHP Video Tutorial"
The above is the detailed content of How to solve php emoji mysql error problem. For more information, please follow other related articles on the PHP Chinese website!