I want to use the ruby program to read the mysql database written by a php program and find that all Chinese characters are garbled, similar to the following: å ˆ ä ä½ ³. If mysqlquerybrowser is used for connection, it is also garbled in mysql. I have read that the database encoding is UTF-8, and php reads the display... I want to use the ruby program to read the mysql database written by a php program and find that all Chinese characters are garbled, similar to the following:
刘佳
.
If mysql query browser is used for connection, garbled characters are displayed in mysql,
I can see that the database encoding is UTF-8, And the php reading is displayed on the page, but the Chinese is displayed normally.
Why is ruby garbled? Is there any way to solve this problem?
require 'active_record'class Student < ActiveRecord::BaseendActiveRecord::Base.establish_connection( adapter: 'mysql2', host: 'xxxx', username: 'xxxx', password: 'xxxx', database: 'xxx_db', encoding: 'utf8')puts Student.first.name
Output result刘佳
PHP displays correct resultsLiu Jia
Reply content:
I want to use the ruby program to read the mysql database written by a php program and find that all Chinese characters are garbled, similar to the following:刘佳
.
If mysql query browser is used for connection, garbled characters are displayed in mysql,
I can see that the database encoding is UTF-8, And the php reading is displayed on the page, but the Chinese is displayed normally.
Why is ruby garbled? Is there any way to solve this problem?
require 'active_record'class Student < ActiveRecord::BaseendActiveRecord::Base.establish_connection( adapter: 'mysql2', host: 'xxxx', username: 'xxxx', password: 'xxxx', database: 'xxx_db', encoding: 'utf8')puts Student.first.name
Output result刘佳
PHP displays correct resultsLiu Jia
>>> "Liu Jia". encode ('utf-8'). decode ('latin1') 'latency \ x88 \ x98 ä½ ³'
Decoded with latin.