in reply to Simpler DBI::MySQL queries

What you are probably thinking about is $dbh->selectrow_array() which can get a single scalar with a single call. It combines prepare,execute,fetch into one call and returns an array in list context and a scalar in scalar context (it is *not* like a normal array that returns the number of elements of the array in scalar context). But you need to be careful because you must make sure that the query returns only one column and one row.

The example below is ok because it asks for a single column and (assuming that the ids are unique, only a single row):

my $username = $dbh->selectrow_array(" SELECT username FROM tbl WHERE id = 9 ");
But this next example is bad, it retrieves all columns and while it *may* work, exactly which column is returned is not guaranteed:
my $username = $dbh->selectrow_array(" SELECT * FROM tbl WHERE id = 9 ");
And this last example is only ok if you want the first row, but not the rest, since it may gather many rows and the selectrow_array will only return the first (note the less than comparison):
my $username = $dbh->selectrow_array(" SELECT username FROM tbl WHERE id < 9 ");

Replies are listed 'Best First'.
Re^2: Simpler DBI::MySQL queries
by Cody Pendant (Prior) on Dec 02, 2004 at 00:44 UTC
    Thanks everyone. That's what I needed. Also I now know that 'perldoc DBI' was the place to look. My code is now simpler.


    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss')
    =~y~b-v~a-z~s; print
Re^2: Simpler DBI::MySQL queries
by Anonymous Monk on Dec 02, 2004 at 04:58 UTC
    Manual blindness, I call it, where you read the perldoc over and over and yet can never find the call you think must be there somewhere. Ta muchly.