Home >Backend Development >PHP Problem >How to solve php emoji mysql error problem

How to solve php emoji mysql error problem

藏色散人
藏色散人Original
2021-09-01 09:03:022510browse

php 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 php emoji mysql error problem

##The operating environment of this article: Windows7 system, PHP7.1 version, Dell G3 computer

How to solve the php emoji mysql error problem?

When making a small program, it is useful to save the user's WeChat name and other information into the database as a practice record for reference, but found that when there is an emoji expression in the WeChat name, the expression cannot be stored in the database field. When I used the access database before, emoticons could be directly stored in the fields.

Cause troubleshooting:

First of all: I tested whether it was caused by the PHP program. I used post to submit content with emoji expressions. The submitted content was normal, and the content in the post was There are emojin expressions.

How to solve php emoji mysql error problem

Next: Try to insert this data into the database. After successfully inserting into the database, I found that the expression changed to a ? symbol.

How to solve php emoji mysql error problem

The problem is basically understood at this point, that is, my database does not support this expression.

Notice! Notice! Note: This is the result of running it using the sql-front tool, but running it in a PHP program may cause all the content behind the emoji expression to not be saved normally. For example:

["Emoji" title]The emoji symbol is at the beginning of the content, and the inserted content is directly empty,

[The previous "Emoji" title]The inserted content is [Front],

Emoji expressions and the content behind the emoji expressions cannot be inserted normally.

Solution:

Method 1: Check the Internet and say that it is to change the database character set encoding. Try changing the character set to utfmb4. Insert another piece of data and see that the expression is indeed stored in the database.

How to solve php emoji mysql error problem

It should be noted that even if the character set of the database is changed, the PHP program must change the character set accordingly. Otherwise it’s still???.

The definition method is: mysqli_query($conn,"SET NAMES UTF8MB4");

How to solve php emoji mysql error problem

Method 2: Escape the emoji expression and convert it when calling go back. It's nothing more than defining two functions, one for escaping and one for anti-escaping. //emoji escaping

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:

How to solve php emoji mysql error problem

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn