Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Mysql select and store one field

by Anonymous Monk
on Sep 07, 2011 at 19:52 UTC ( #924654=perlquestion: print w/replies, xml ) Need Help??

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!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2022-10-04 08:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (16 votes). Check out past polls.

    Notices?