How does ruby read garbled data from the mysql database saved by the php program?

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.