http://www.perlmonks.org?node_id=924654

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

Is there an easier way to recieve one piece of data from a mysql query? I really am only selecting one field. (SELECT post FROM tablename WHERE url="$url").

All I'm trying to do is get that variable to $this. All the references I see online show
while (@row = $sth->fetchrow_array) { print "@row\n" }
Which seems above and beyond what I need it to do. Is there any better way to do what I need?

Replies are listed 'Best First'.
Re: Mysql select and store one field
by moritz (Cardinal) on Sep 07, 2011 at 19:54 UTC
Re: Mysql select and store one field
by nedals (Deacon) on Sep 08, 2011 at 00:12 UTC
    my ($post) = $dbh->selectrow_array("SELECT post FROM tablename WHERE u +rl=?", {}, $url);
    Uses placeholders to prevent SQL injection but I would still validate the $url if it's comming from outside the script.
    This assumes you are only expecting one row to be returned.
Re: Mysql select and store one field
by suaveant (Parson) on Sep 07, 2011 at 20:01 UTC
    or
    print $sth->fetchrow_array,"\n";

                    - Ant
                    - Some of my best work - (1 2 3)

Re: Mysql select and store one field
by Anonymous Monk on Sep 07, 2011 at 21:02 UTC
    Also, to avoid the Bobby Tables problem, be sure to pass the content of $url as a parameter to the query .. never, ever, EVER by direct inclusion into a string variable!