garbled characters in PHP+MySQL are not fixed

Asked 2 years ago, Updated 2 years ago, 131 views

Background
We have decided to transfer the original system from one server to another.

Export

mysqldump-u root-p DBNAME --default-character-set=binary>~/DBNAME.sql 

import
I imported it using phpMyAdmin because I couldn't type the command directly.
At this time, I tried utf8, sjis, binary, etc. for the "File Character Set:", but the results have not changed.

Test connection with Perl
When Perl connects to the database, there is no garbled characters in Japanese, and the migrated data is read beautifully.This is UTF8.
However, the actual system is PHP, so it was only a confirmation of operation.

Connect via PHP
When connected via PHP, the characters were already garbled at phpMyAdmin, and the desired system only has Japanese characters, so there is no sign of healing.

Tried
I tried it by using internet search.

  • When exporting, I tried sjis, utf8, euc, etc. instead of binary.
  • We also made possible character code combinations during import
  • Since SQL on phpMyAdmin also causes textual damage, after several attempts with the "SET NAMES utf8" command at the beginning, the type of garbled characters in the output changes, but no combination is displayed properly
  • Change the default character code in the database and try importing again, but the result remains the same

information
MySQL:5.5.28
phpMyAdmin —3.3.10.5
PHP:PHP 5.4.40

That's all.
Thank you for your cooperation.

php mysql garbled-characters

2022-09-30 11:55

1 Answers

I probably solved it myself, so I will answer it.

After adding the following code to the beginning, you can now display it without garbled characters.

mysql_set_charset('utf8',$con);

This post was posted as a community wiki based on what @Monon added to the body of the question.


2022-09-30 11:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.