开发者

Select using MySQL shows Mysql::Result, not my value

I am trying to make a simple command line user authentication system in Ruby.

I am using MySQL to store user information.

I decided to store the passwords as MD5 hashes, mainly because it is simple.

I am not going for anything crazy secure at the mome开发者_高级运维nt, I just want it to work.

However when I use a select statement in Ruby to pull the hash out so I can compare it with a hash of whatever the user entered it does not come out in the same 32 character format!

When I use MySQL to select the hash I get:

 SELECT password FROM users WHERE username = 'admin';
 +----------------------------------+
 | password                         |
 +----------------------------------+
 | d456c1f6fa85ed2f6a26b15139c6ddc2 |
 +----------------------------------+

When I use Ruby however..

 con.query("SELECT password FROM users WHERE username = 'admin'")
 #<Mysql::Result:0x950b208>

I haven't ever really programmed anything before, and am really enjoying Ruby.

I have been googling this for two days now and can't seem to find anything.

I am sorry if this is a really easy fix, but I cant seem to find it anywhere.

I would also love suggestions about a better way to go about this little project! Thanks in advance :)


It looks like you need to just process the results of the query. The return value of query is a result set object:

rows = con.query("SELECT password FROM employee WHERE username = 'admin'")
rows.each do |row|
   puts row[0]
end


Try

rows.map(&:password) 

that should work too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜