search
HomeBackend DevelopmentPHP Tutorialruby怎么读取php程序保存的mysql数据库里面的乱码数据

我想使用ruby程序读取一个php程序写入的mysql数据库,发现都出来的中文是乱码,类似如下的:刘佳

如果用mysql query browser连接,在mysql里面显示也是乱码,
我看了下此数据库编码是utf-8,而且php读取显示在页面上,中文却能正常显示。

那ruby读取出来为什么是乱码呢? 有什么方法可以解决这个问题吗?

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

输出结果 刘佳
PHP显示的正确结果 刘佳

回复内容:

我想使用ruby程序读取一个php程序写入的mysql数据库,发现都出来的中文是乱码,类似如下的:刘佳

如果用mysql query browser连接,在mysql里面显示也是乱码,
我看了下此数据库编码是utf-8,而且php读取显示在页面上,中文却能正常显示。

那ruby读取出来为什么是乱码呢? 有什么方法可以解决这个问题吗?

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

输出结果 刘佳
PHP显示的正确结果 刘佳

<code>>>> "刘佳".encode('utf-8').decode('latin1')
'å\x88\x98ä½³'
</code>

以latin解码了。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php怎么去除首位数字php怎么去除首位数字Apr 20, 2022 pm 03:23 PM

去除方法:1、使用substr_replace()函数将首位数字替换为空字符串即可,语法“substr_replace($num,"",0,1)”;2、用substr截取从第二位数字开始的全部字符即可,语法“substr($num,1)”。

为什么 Python、Ruby 等语言弃用了自增运算符?为什么 Python、Ruby 等语言弃用了自增运算符?May 11, 2023 pm 04:37 PM

许多人也许会注意到一个现象,那就是在一些现代编程语言(当然,并不是指“最近出现”的编程语言)中,自增和自减运算符被取消了。也就是说,在这些语言中不存在​​i++​​​或​​j--​​​这样的表达,而是只存在​​i+=1​​​或​​j-=1​​这样的表达方式了。本回答将从设计哲学这个角度上探讨这一现象产生的背景与原因。严格来说,说"i++正在消失"也许有失偏颇,因为主流编程语言中似乎只有Python、Rust和Swift不支持自增自减运算符。​当我第一次接触Python时,这也

php有操作时间的方法吗php有操作时间的方法吗Apr 20, 2022 pm 04:24 PM

php有操作时间的方法。php中提供了丰富的日期时间处理方法:1、date(),格式化本地日期和时间;2、mktime(),返回日期的时间戳;3、idate(),格式化本地时间为整数;4、strtotime(),将时间字符串转为时间戳等等。

深入分析 Golang 与 Ruby 的异同深入分析 Golang 与 Ruby 的异同Jun 01, 2024 pm 08:46 PM

Go与Ruby的主要区别在于:Go是一种静态类型编译语言,支持轻量级并行和高效内存管理,适合编写高并发应用程序;Ruby是一种动态类型解释语言,支持真正的并行但内存管理需手动控制,适合编写灵活的Web应用程序。

php怎么给数组增加一个数组元素php怎么给数组增加一个数组元素Apr 19, 2022 pm 08:45 PM

增加元素的方法:1、使用“array_unshift(数组,数组元素)”语句,在数组的开头添加元素;2、使用“array_push(数组,数组元素)”语句,在数组的末尾添加元素;3、用“array_pad(数组,数组长度+1,元素)”语句。

Ruby怎么使用Mysql2连接操作MySQLRuby怎么使用Mysql2连接操作MySQLApr 17, 2023 pm 10:07 PM

Ruby操作MySQL使用mysql2连接mysql并操作mysql。geminstallmysql2连接mysql建立连接:require&#39;mysql2&#39;conn=Mysql2::Client.new({host:&#39;192.168.200.73&#39;,username:&#39;root&#39;,password:&#39;P@ssword1!&#39;})接受的连接选项包括:Mysql2::Clie

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools