# Mysql ruby prob

**URL:** <https://www.ruby-forum.com/t/mysql-ruby-prob/132774>\
**Category:** Ruby\
**Created:** [March 24, 2008, 4:55pm UTC](https://www.ruby-forum.com/t/mysql-ruby-prob/132774 "2008-03-24T16:55:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 24, 2008, 4:55pm UTC](https://www.ruby-forum.com/t/mysql-ruby-prob/132774/1 "2008-03-24T16:55:55Z")

</div>

Hi,

after hours of reading and searching i can’t get the solution  
code:

require ‘mysql’  
my = Mysql.new(“localhost”, “user”, “”, “database”)  
data = my.query(“select \* from books Limit 5”)  
p “first time”  
data.each { |d| p d[0]}  
p “again”  
data.each { |d| p d[0]}

Result:  
“first time”  
“7”  
“9”  
“11”  
“12”  
“14”  
“again”

I expected that the result would be:

“first time”  
“7”  
“9”  
“11”  
“12”  
“14”  
“again”  
“7”  
“9”  
“11”  
“12”  
“14”

I get this result only if i add

require ‘mysql’  
my = Mysql.new(“localhost”, “user”, “”, “database”)  
data = my.query(“select \* from books Limit 5”)  
p “first time”  
data.each { |d| p d[0]}  
p “again”  
data = my.query(“select \* from books Limit 5”) \<\< ADDED  
data.each { |d| p d[0]}

So i think thats not the rigth way - don’t repeat yourself

Thanks in advance  
DG

--------------= Posted using GrabIt =----------------  
------= Binary Usenet downloading made easy =---------  
-= Get GrabIt for free from [http://www.shemes.com/](http://www.shemes.com/) =-

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 24, 2008, 5:10pm UTC](https://www.ruby-forum.com/t/mysql-ruby-prob/132774/2 "2008-03-24T17:10:07Z")

</div>

On Tue, Mar 25, 2008 at 12:55:02AM +0900, abc- wrote:

> after hours of reading and searching i can’t get the solution  
> code:
> 
> require ‘mysql’  
> [iterating twice through the same result set, expecting the rows twice]
> 
> So i think thats not the rigth way - don’t repeat yourself

The result set returned by the query is not an array of rows. It is an  
interface to reading the results row by row without storing all of them  
in  
memory at once. This means that as you iterate, it throws out the  
previous  
row. If you want an array of rows, through which you can iterate  
as often as you like, use this:

require ‘mysql’  
my = Mysql.new(“localhost”, “user”, “”, “database”)  
data = my.query(“select \* from books Limit 5”).extend(Enumerable).to\_a

# …

> Thanks in advance  
> DG  
> –Greg
