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 ");

In reply to Re: Simpler DBI::MySQL queries by jZed
in thread Simpler DBI::MySQL queries by Cody Pendant

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.