# Reading MySQL rows

**URL:** <https://www.ruby-forum.com/t/reading-mysql-rows/57524>\
**Category:** Rails\
**Created:** [April 11, 2006, 6:36pm UTC](https://www.ruby-forum.com/t/reading-mysql-rows/57524 "2006-04-11T18:36:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![msojka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@msojka](https://www.ruby-forum.com/u/msojka)\
**Post date:** [April 11, 2006, 6:36pm UTC](https://www.ruby-forum.com/t/reading-mysql-rows/57524/1 "2006-04-11T18:36:16Z")

</div>

Hi there,

I´m having difficulties reading the content of some rows in my database.  
Tried to use some of the code at page 221 in the pickaxe, but didn´t  
make it work.

The SQL query works fine directly against the database.  
mysql\> select id, count, company from users where name = “martin”;  
±—±------±-----------+  
| id | count | company |  
±—±------±-----------+  
| 1 | 0 | |  
±—±------±-----------+

def count(number)  
puts "count() reached!\n user is #{@username} " # this works

userdata = User.find\_by\_sql("select id, count, company from users  
where name = ‘@username’ ")  
account = userdata[0]  
puts “account-id: #{@account.id}” # empty  
puts “the name of the company is: #{@account.company}, count #  
{@account.count}\n” # empty

end

How do I collect the content of the id, count and company field to  
variables?

I´m very grateful for all help I can get!

–  
Best regards,  
Martin S.\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
Rails mailing list  
removed\_email\_address@domain.invalid  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

---

<div class="post-metadata">

**Author:** ![msojka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@msojka](https://www.ruby-forum.com/u/msojka)\
**Post date:** [April 11, 2006, 7:19pm UTC](https://www.ruby-forum.com/t/reading-mysql-rows/57524/2 "2006-04-11T19:19:13Z")

</div>

Martin S. wrote:

> end  
> Your last two puts lines reference @account, but the line before assigns  
> to account. @account will be nil unless assigned already in the current  
> object.

Also, the SQL sent to the database will currently contain the literal  
string “@username” as the name rather than the contents of @username.  
The best way to pass parameters to the database is with one of the  
following two techniques:

userdata = User.find\_by\_sql([“select id, count, company from users where  
name = ?”, @username])

userdata = User.find\_by\_sql([“select id, count, company from users where  
name = :name”, {:name =\> @username})

–  
Philip R.  
[http://tzinfo.rubyforge.org/](http://tzinfo.rubyforge.org/) – DST-aware timezone library for Ruby

---

<div class="post-metadata">

**Author:** ![msojka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@msojka](https://www.ruby-forum.com/u/msojka)\
**Post date:** [April 11, 2006, 8:59pm UTC](https://www.ruby-forum.com/t/reading-mysql-rows/57524/3 "2006-04-11T20:59:07Z")

</div>

Hi,

On Apr 11, 2006, at 7:14 PM, Philip R. wrote:

> > end  
> > userdata = User.find\_by\_sql([“select id, count, company from users  
> > where name = ?”, @username])
> 
> userdata = User.find\_by\_sql([“select id, count, company from users  
> where name = :name”, {:name =\> @username})

Thanks!

I tried with this:

```
userdata = User.find_by_sql(["select id, sendt, company from

```

users where name = ?", @username])

@account = userdata[0]  
puts “id is: #{@account.id}, sendt: #{@account.sendt}}”

How should I write it to make puts print the right id and sent values?

Thanks again!

–  
Regards,  
Martin S.

---

<div class="post-metadata">

**Author:** ![msojka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@msojka](https://www.ruby-forum.com/u/msojka)\
**Post date:** [April 11, 2006, 9:05pm UTC](https://www.ruby-forum.com/t/reading-mysql-rows/57524/4 "2006-04-11T21:05:22Z")

</div>

Hi,

On Apr 11, 2006, at 8:57 PM, Martin S. wrote:

> > > {@account.count}\n" # empty
> 
> userdata = User.find\_by\_sql([“select id, sendt, company from  
> users where name = ?”, @username])
> 
> @account = userdata[0]  
> puts “id is: #{@account.id}, sendt: #{@account.sendt}}”
> 
> How should I write it to make puts print the right id and sent values?

I forgot to mention the most important detail, @account.id and  
@account.sendt is still empty. 🙂

Sorry for not getting it right in the first email!

Regards,  
Martin

---

<div class="post-metadata">

**Author:** ![msojka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@msojka](https://www.ruby-forum.com/u/msojka)\
**Post date:** [April 12, 2006, 11:11am UTC](https://www.ruby-forum.com/t/reading-mysql-rows/57524/5 "2006-04-12T11:11:17Z")

</div>

Martin S. wrote:

> I forgot to mention the most important detail, @account.id and  
> @account.sendt is still empty. 🙂

I’m not sure what the problem is here. There’s an extra close brace at  
the end of your puts line, but this shouldn’t be causing any problem.

Perhaps you could try putting the following line in after running the  
query to see what is returned:

puts userdata.inspect

–  
Philip R.  
[http://tzinfo.rubyforge.org/](http://tzinfo.rubyforge.org/) – DST-aware timezone library for Ruby

---

<div class="post-metadata">

**Author:** ![msojka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@msojka](https://www.ruby-forum.com/u/msojka)\
**Post date:** [April 12, 2006, 1:08pm UTC](https://www.ruby-forum.com/t/reading-mysql-rows/57524/6 "2006-04-12T13:08:15Z")

</div>

On Tuesday, April 11, 2006, at 6:33 PM, Martin S. wrote:

> ±—±------±-----------+  
> account = userdata[0]  
> Iï¿½m very grateful for all help I can get!
> 
> –  
> Best regards,  
> Martin S.\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

You should be able to do something like this:

userdata = User.find\_by\_name(:first, @username)

The “by\_name” compares @username with your “name” column. All columns  
are returned. Then you examine columns like: “userdata.company”.

I think your orginal query will work if you change it slightly.

userdata = User.find\_by\_sql(“select id, count, company from users  
where name = ‘#{@username}’”.
