Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
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 ( #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
Node Status?
node history
Node Type: perlquestion [id://565865]
Approved by GrandFather
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2018-06-23 05:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (125 votes). Check out past polls.

    Notices?