Different coding problem when recording data via PHP project or direct from Mysql

Asked

Viewed 158 times

1

Friends, all right?

I’m learning about PHP and when testing connections with Mysql I noticed a curious problem:

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

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

Note that in both cases I can view the information with 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 was inserted.

When creating the database I used the following sequence of commands: CREATE DATABASE nome_banco DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;

For the tables I used the following sequence of commands: 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 hint of what might be going on?

1 answer

0

Mysql’s "utf8" is not quite UTF-8, as it only supports 3 bytes per character, while real UTF-8 - which everyone uses, including your PHP application - requires 4 bytes per character.

This is one of the probable causes of their divergence.

If your Mysql version is higher 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;

That one article (English) explains the whole problem well.

  • Juliano, thanks for the help. I think my case goes a little further. In the table I have no problems. The curiosity is that if I insert the data through my application in PHP and visualize in my application the accents appear normal, but if I visualize the same record inside the database through phpMyAdmin, then the accents are not recognized. In the same way, if I insert the record in the bank through phpMyAdmin and visualize by Myadmin itself the accents are displayed normally, but if I try to check this record by my application, then the accents get corrupted.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.