skip to Main Content

I have a php script that copies data from 1 table to another.
The source table and its fields are utf8mb4_general_ci while the target table and its fields are utf8mb4_unicode_ci.

I get an "Incorrect string value" each time the script tries to insert a special characte like "è".

The DataBase version is 8.0.36-28. The script used to wok well with 5.6.51-91.0-log DB version.
I tryed with and without mb_convert_encoding, the result is the same.

2

Answers


  1. Different encoding can be a real pain.
    And confusing.

    I advise you to have a look at iconv(). it can change encoding1 to encoding2:

    iconv

    Or, if you want to dive a bit deeper, follow the link by KIKO software in the comment.

    Login or Signup to reply.
  2. utf8mb4_general_ci and utf8mb4_unicode_ci are different _COLLATIONs_ of the same encodingutf8mb4.

    If you are bringing the data into PHP and then sending it back out, the likely villain is found in SHOW VARIABLES LIKE 'char%'; Please run that and show us the result.

    The best fix is to connect with utf8mb4; second best is to run SET NAMES utf8mb4; right after connecting.

    mb_convert_encoding (and any other conversion tool) will only make things worse.

    Simply using SQL to migrate the data may be better:

    INSERT INTO new_table
        SELECT * FROM old_table;
    

    or

    INSERT INTO new_table (col1, col2, ...)
        SELECT col1, col2, ... FROM old_table;
    

    The latter syntax lets you add/remove columns (etc) at the same time as you copy the data. However, any expressions may involve the COLLATION and cause trouble.

    Let’s see your "copy" code.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search