0

I think I'm being stupid.

rc = Mysql.new('CENSORED_HOST','username','password','db')
release = rc.query('select * from wp_ribcage_releases where release_id = 1 limit 1')

puts release["release_title"]

rc.close

And I am getting the following error:

ribcage-connect.rb:17: undefined method `[]' for #<Mysql::Result:0x1011ee900> (NoMethodError)

I'm new to this and I am sure I am basically doing something very stupid. Thanks a lot.

EDIT

The kind people below have got me a but further, now I have:

rc = Mysql.new('server','user','pword','db')

release = rc.query('select * from wp_ribcage_releases where release_id = 1 limit 1')

row = release.fetch_row
puts row['release_title']

rc.close

And now I get the error:

ribcage-connect.rb:12:in `[]': can't convert String into Integer (TypeError)
    from ribcage-connect.rb:12

2 Answers 2

2

the rc.query(...) returns a result set you can iterate. You can't directly access it.

Look at:

http://www.kitebird.com/articles/ruby-mysql.html#TOC_7

Since you have only one row (because of LIMIT 1) you can do:

row = release.fetch_hash
puts row['release_title']

If you have multiple rows:

while row = release.fetch_hash do
 puts row["release_title"]
end
Sign up to request clarification or add additional context in comments.

3 Comments

I not getting this problem - release = rc.query('select * from wp_ribcage_releases where release_id = 1 limit 1') release = release.fetch_row puts release['release_artist'] = can't convert String into Integer (TypeError) from ribcage-connect.rb:12
@The Warm Jets: It's fetch_hash if you want to use column names. fetch_row will use column indexes. Also, you should use a different variable instead of overwriting release, for clarity.
@sled: You have a bug in your first sample, it should be fetch_hash
0

That error means you cannot index directly (use []) the Result object.

It should instead be (for this single row case):

row = release.fetch_hash
puts row["release_title"]

For more information: go here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.