Resolving incorrect character encoding when displaying MySQL database results after upgrade to PHP 5.3

前端 未结 3 539
天涯浪人
天涯浪人 2021-01-04 19:19

Issue Description

After upgrading PHP on our development server from 5.2 to 5.3, we\'re encountering an issue where data requested from our database and displayed

相关标签:
3条回答
  • 2021-01-04 19:33

    I see you've tried this, but the syntax I use is: mysql_query("SET NAMES utf8"). Your syntax may be correct, I've just never seen it like that before.

    Example:

    // connect to database stuff
    $Connection = mysql_connect($server, $username, $password)
    or die ("Error connecting to server");
    
    // connect to database stuff
    $db = mysql_select_db($database, $Connection)
    or die ("Error selecting database");
    
    mysql_query("SET NAMES utf8");
    
    0 讨论(0)
  • 2021-01-04 19:55

    I had similar issue after upgrading PHP from 5.2.3 to 5.3.5 (5.3.5-Win32-VC6-x86), MySQL 5.0.41 (not updated). I think that reason is a little difference between PHP versions.

    PHP 5.2.3 default (without SET NAMES):
    character_set_client = latin1
    character_set_connection = latin1
    character_set_database = utf8
    character_set_filesystem = binary
    character_set_results = latin1
    character_set_server = latin2
    character_set_system = utf8
    collation_connection = latin1_swedish_ci
    collation_database = utf8_polish_ci
    collation_server = latin2_general_ci

    PHP 5.3.5 default (without SET NAMES):
    character_set_client = latin2
    character_set_connection = latin2
    character_set_database = utf8
    character_set_filesystem = binary
    character_set_results = latin2
    character_set_server = latin2
    character_set_system = utf8
    collation_connection = latin2_general_ci
    collation_database = utf8_polish_ci
    collation_server = latin2_general_ci

    I added data to database in PHP 5.2.3 default (without SET NAMES), so now to display it correctly I must read it using:

    $pdo -> query("SET NAMES 'latin1'");
    

    Maybe something similar is reason of your problem.

    0 讨论(0)
  • 2021-01-04 19:56

    If you have made sure that both the tables, and the output encoding are UTF-8, almost the only thing left is the connection encoding.

    The reason for the change in behaviour when updating servers could be a change of the default connection encoding:

    [mysql]
    default-character-set=utf8
    

    However, I can't see any changes in the default encoding between versions, so if those were brand-new installs, I can't see that happening.

    Anyway, what happens if you run this from within your PHP query and output the results. Any differences to the command line output?

     SHOW VARIABLES LIKE 'character_set%';
     SHOW VARIABLES LIKE 'collation%'; 
    
    0 讨论(0)
提交回复
热议问题