Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

by Anonymous Monk
on Aug 06, 2006 at 21:30 UTC ( [id://565865]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

was just wondering the quickest method? Should I even bother using a prepare cache statement for the query? I'm thinking no since I'm only returning one row and one column.

I read some of the tutorials here but still confused. None actually give you this situation where you only need one column and one row returned in a sql query. Not sure where to look and was wondering out of curiosity. Any suggestions would be nice.

Thanks.
  • Comment on Quickest way of fetching a Single row and column in MySQL using DBI

Replies are listed 'Best First'.
Re: Quickest way of fetching a Single row and column in MySQL using DBI
by cchampion (Curate) on Aug 06, 2006 at 22:00 UTC
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
    Nvm...found it.

    for future ref.
    my $id = $dbh->selectrow_arrayref("SELECT id FROM users where username + = '$username'");

      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);
Re: Quickest way of fetching a Single row and column in MySQL using DBI
by runrig (Abbot) on Aug 07, 2006 at 16:39 UTC
    Should I even bother using a prepare cache statement for the query?

    That depends on whether or not you're executing the statement multiple times. In MySQL, you get less benefit in pre-preparing your statements than in some other databases, but the answer is that you can prepare it once if you want to since selectrow_array() (which there are other examples of in this thread) accepts either a SQL statement or a previously prepared statement handle as it's first argument.

    Also, depending on how often you're executing the lookup in your program, and how large the table is, you may want to just read the whole table into a hash array.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://565865]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-12-12 16:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (65 votes). Check out past polls.