Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Quickest way of fetching a Single row and column in MySQL using DBI

by Anonymous Monk
on Aug 06, 2006 at 21:38 UTC ( #565866=note: print w/replies, xml ) Need Help??


in reply to Quickest way of fetching a Single row and column in MySQL using DBI

Nvm...found it.

for future ref.
my $id = $dbh->selectrow_arrayref("SELECT id FROM users where username + = '$username'");
  • Comment on Re: Quickest way of fetching a Single row and column in MySQL using DBI
  • Download Code

Replies are listed 'Best First'.
Re^2: Quickest way of fetching a Single row and column in MySQL using DBI
by cees (Curate) on Aug 07, 2006 at 01:04 UTC

    Although your version works, it is potentially open to an SQL injection attack through the $username variable. With DBI that is easy to avoid by using placeholders. Also, I think you want selectrow_array (selectrow_arrayref returns a reference to an array, wheras selectrow_array will return a list, or just a scalar value if you are selecting a single column).

    my $id = $dbh->selectrow_array("SELECT id FROM users where username = +?", {}, $username);
      I think you need array context there:
      my ($id) = $dbh->selectrow_array("SELECT id FROM users where username += ?", {}, $username);

        Scalar context works as well according to the DBI source code:

        sub selectrow_array { my $row = _do_selectrow('fetchrow_arrayref', @_) or return; return $row->[0] unless wantarray; return @$row; }

        The docs hint that this should work as well, but don't explicitly state it. Instead the docs mention this:

        If called in a scalar context for a statement handle that has more than one column, it is undefined whether the driver will return the value of the first column or the last. So don't do that.

        Anyway, it won't hurt to add the brackets in there to force list context, but it shouldn't be required.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://565866]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2018-07-17 03:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (354 votes). Check out past polls.

    Notices?