I want to use ruby to read/insert data to a mysql database, onto which data were saved by a php code. When I read Chinese data, it does not appear correctly. It appears like 刘佳
. But in a php page, it shows Chinese data correctly as 刘佳
.
I confirmed the database uses utf-8 charset (CHARSET=utf8 COLLATE=utf8_unicode_ci
).
my ruby code
require 'active_record'
class Student < ActiveRecord::Base
end
ActiveRecord::Base.establish_connection(
adapter: 'mysql2',
host: 'xxxx',
username: 'xxxx',
password: 'xxxx',
database: 'xxx_db',
encoding: 'utf8'
)
puts Student.first.name
It outputs an unknown string "刘佳"
.
How can I read Chinese data correctly and save a new Chinese record to database?
Copyright Notice:Content Author:「Grant Chen」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/24473907/read-insert-chinese-data-from-mysql-database-saved-by-php-code