Different coding problem when registering data via PHP project or directly through MySQL [duplicate]

1

Friends, how are you?

I'm learning about PHP and testing connections to MySQL I noticed a curious problem:

If I am using phpMyAdmin and through it I insert records, with accentuation, in my table the information is normally displayed when performing a SELECT. However, when displaying this data in my PHP project they do not display the accents and instead have those characters already well known in this situation.

On the other hand, if I insert the data through my PHP project the display usually occurs with the accents, but by pulling the SELECT within phpMyAdmin the record returns with error in coding these accents.

Notice that in both cases I can view the information with the accented characters and on the same screen there will be records with corrupted characters, either in my PHP project or through the phpMyAdmin interface, having as their differential the interface by which the record has been entered.

When I created the database I used the following script: CREATE DATABASE nome_banco DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;

For the tables I used the following script: CREATE TABLE nome_tabela ( ... ) DEFAULT CHARSET = utf8;

And in my PHP project, I use a template file that takes in HTML: <!DOCTYPE html> <html lang="pt-BR"> <head> <meta charset="UTF-8" />

Does anyone have any tips on what might be happening?

    
asked by anonymous 29.05.2018 / 22:37

1 answer

0

MySQL "utf8" is not quite UTF-8, as it only supports 3 bytes per character, while true UTF-8 - which everyone uses, including their PHP application - needs 4 bytes per character. p>

This is one of the likely causes of their divergence.

If your MySQL version is greater than 4.1, run a test using utf8mb4 in the encoding of your MySQL, which implements the 4-byte version of UTF-8.

CREATE DATABASE nome_banco
DEFAULT CHARACTER SET utf8mb4
DEFAULT COLLATE utf8mb4_unicode_ci;

CREATE TABLE nome_tabela (
    ...
) DEFAULT CHARSET = utf8mb4;

This article (English) explains the whole problem right away. .

    
30.05.2018 / 02:46