简体   繁体   中英

mysql php UTF-8

There's a strange problem here:

i have to write data from a CSV file into a mysql DB (default charset UTF-8)

when i read the CSV and print the query string on terminal (for debug purpose )the output is correct:

insert into orari (pv_id, day) values(5492, 'giovedì pomeriggio')

but when i run a select query with mysql client the result is weird:

*************************** 273. row ***************************
             id: 28856
             day: giovedì pomeriggio
             pv_id: 5760

Same thing if i run the query on phpmyadmin.

The HTML output is correct, the charset is set to UTF-8 and the characters like àòù are correctly visualized.

* Edit * This is my mysql charset and collation settings:

SHOW VARIABLES LIKE 'c%';

| character_set_client     | utf8                                                  |
| character_set_connection | utf8                                                  |
| character_set_database   | utf8                                                  |
| character_set_filesystem | binary                                                |
| character_set_results    | utf8                                                  |
| character_set_server     | utf8                                                  |
| character_set_system     | utf8                                                  |
| collation_connection     | utf8_general_ci                                       |
| collation_database       | utf8_general_ci                                       |
| collation_server         | utf8_general_ci                                       |

Each MySQL client should specify the charset they want when they handshake with the server. Unless overidden the default is latin1 .

You can issue the following query on every connection to work around this:

SET NAMES 'utf8';

Or you can set it in the MySQL charset configuration as described in the MySQL manual.

You say that the HTML and the input is correct, but only mysql and phpmyadmin are incorrect. You need to set the proper connection charset for these as well, since they default to latin1 which means "8-bit clean, one byte per char".

(This shouldn't affect your problem, but you should make sure your table or column specify utf-8 storage.)

You'll notice your garbage output is two bytes, which means that what is stored in the DB is definitely UTF8. Your data is being misinterpreted on output.

For the mysql terminal program, start it with the --default-character-set parameter. Make sure it matches your terminal's character set.

I don't know how this is set with phpMyAdmin, but there should be a setting for connection character set as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM