# Getting first value of a mysql return

**URL:** <https://www.ruby-forum.com/t/getting-first-value-of-a-mysql-return/69759>\
**Category:** Ruby\
**Created:** [September 23, 2006, 10:29pm UTC](https://www.ruby-forum.com/t/getting-first-value-of-a-mysql-return/69759 "2006-09-23T22:29:53Z")\
**Posts on this page:** 3\
**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:** [September 23, 2006, 10:29pm UTC](https://www.ruby-forum.com/t/getting-first-value-of-a-mysql-return/69759/1 "2006-09-23T22:29:53Z")

</div>

hi list -

cant find any decent mysql/ruby usage examples, any suggestions? even  
the dave thomas book has very little…

i have a sql statement which sometimes returns a single value, and  
sometimes it cant find a matching result.

how do i trap this nothing value? it doesnt seem to be a “nil” : the  
return type is always:  
MySql::Result

which is not a hash, so i cant get the keys or count() or anything…

puts res.class  
# Mysql::Result

itemCount = res.keys.length  
#\> NoMethodError: undefined method ‘keys’ for #

if ( res.nil? ) // never is tho…

–  
also how do i extrac the first and only result? eg if the result is  
not a list of items, but just reding the value of one field?

tx!

## –

```
  David "DC" Collier

```

[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
+81 (0)80 6521 9559  
skype: callto://d3ntaku

---

<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:** [September 24, 2006, 12:41pm UTC](https://www.ruby-forum.com/t/getting-first-value-of-a-mysql-return/69759/2 "2006-09-24T12:41:01Z")

</div>

dc [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> MySql::Result  
> That’s the usual way DB interfaces handle this. You always get a result

- 

but it may be empty. You can either iterate all result rows and notice  
that  
you do not have any result that way. Or you can maybe try #empty? on  
the  
result.

Kind regards

```
robert

```

---

<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:** [October 2, 2006, 12:01am UTC](https://www.ruby-forum.com/t/getting-first-value-of-a-mysql-return/69759/3 "2006-10-02T00:01:43Z")

</div>

Robert K. wrote:

> > return type is always:  
> > MySql::Result
> 
> That’s the usual way DB interfaces handle this. You always get a result -  
> but it may be empty. You can either iterate all result rows and notice that  
> you do not have any result that way. Or you can maybe try #empty? on the  
> result.
> 
> Kind regards
> 
> ```
> robert
> 
> ```

Yep, try #num\_rows on the result (I had a look at mysql.rb in rails)

Cheers,  
Andy
