SET NAMES utf8 in MySQL?

前端 未结 8 1408
北海茫月
北海茫月 2020-11-22 11:30

I often see something similar to this below in PHP scripts using MySQL

query(\"SET NAMES utf8\");   

I have never had to do this for any pr

8条回答
  •  一生所求
    2020-11-22 11:54

    Getting encoding right is really tricky - there are too many layers:

    • Browser
    • Page
    • PHP
    • MySQL

    The SQL command "SET CHARSET utf8" from PHP will ensure that the client side (PHP) will get the data in utf8, no matter how they are stored in the database. Of course, they need to be stored correctly first.

    DDL definition vs. real data

    Encoding defined for a table/column doesn't really mean that the data are in that encoding. If you happened to have a table defined as utf8 but stored as differtent encoding, then MySQL will treat them as utf8 and you're in trouble. Which means you have to fix this first.

    What to check

    You need to check in what encoding the data flow at each layer.

    • Check HTTP headers, headers.
    • Check what's really sent in body of the request.
    • Don't forget that MySQL has encoding almost everywhere:
      • Database
      • Tables
      • Columns
      • Server as a whole
      • Client
        Make sure that there's the right one everywhere.

    Conversion

    If you receive data in e.g. windows-1250, and want to store in utf-8, then use this SQL before storing:

    SET NAMES 'cp1250';
    

    If you have data in DB as windows-1250 and want to retreive utf8, use:

    SET CHARSET 'utf8';
    

    Few more notes:

    • Don't rely on too "smart" tools to show the data. E.g. phpMyAdmin does (was doing when I was using it) encoding really bad. And it goes through all the layers so it's hard to find out.
    • Also, Internet Explorer had really stupid behavior of "guessing" the encoding based on weird rules.
    • Use simple editors where you can switch encoding. I recommend MySQL Workbench.

提交回复
热议问题