FAQ
Hello,

I am trying to understand this code from CPAN hepl page:



$sth = $dbh->prepare("SELECT foo, bar FROM table WHERE baz=?"); $sth->execute( $baz ); while ( @row = $sth->fetchrow_array ) { print "@row\n"; }


what is execute doing here with $baz? what is $baz representing?

thank you.
Rajeev

Search Discussions

  • William Gordon Rutherdale (rutherw) at Jul 29, 2011 at 10:20 pm
    You should look up what prepare-statements do in SQL in general.

    The idea is that you define the form of the query or update in advance,
    giving the RDBMS a chance to construct the operation. You then send it
    repeated execute calls with specific values.

    In your example, baz in the prepare call is a column name. $baz in the
    execute call is a value being substituted for the placeholder. The
    question mark is a placeholder.

    -Will

    -----Original Message-----
    From: Rajeev Prasad
    Sent: 29 July 2011 18:14
    To: dbi-users@perl.org
    Subject: question on CPAN example for DBI

    Hello,

    I am trying to understand this code from CPAN hepl page:



    $sth = $dbh->prepare("SELECT foo, bar FROM table WHERE baz=?"); $sth-
    execute( $baz ); while ( @row = $sth->fetchrow_array ) { print
    "@row\n"; }


    what is execute doing here with $baz? what is $baz representing?

    thank you.
    Rajeev
  • Stuart Cooper at Jul 29, 2011 at 10:21 pm
    Hi Rajeev,

    $baz is a variable that gets substituted for the ? in the prepare
    statement, between the $dbh->prepare() and the $sth->execute.

    So it's really:

    $sth = $dbh->prepare("SELECT foo, bar FROM table WHERE baz=?");

    ... extra lines here... calculating $baz

    $sth->execute( $baz );

    Read a bit further down in perldoc DBI and you should hit a paragraph saying
    The "?" characters are placeholders...

    You don't have to use ? placeholders if you know well in advance what
    you're looking for
    and you don't want to run that same query multiple times with
    different baz values.

    $sth = $dbh->prepare("SELECT foo, bar FROM table WHERE baz=5");
    $sth->execute();

    is fine.

    Hope this helps,
    Stuart.
    On Sat, Jul 30, 2011 at 8:13 AM, Rajeev Prasad wrote:
    Hello,

    I am trying to understand this code from CPAN hepl page:



    $sth = $dbh->prepare("SELECT foo, bar FROM table WHERE baz=?"); $sth->execute( $baz ); while ( @row = $sth->fetchrow_array ) { print "@row\n"; }


    what is execute doing here with $baz? what is $baz representing?

    thank you.
    Rajeev
  • Rajeev Prasad at Jul 29, 2011 at 10:30 pm
    Thanks much Stuart, understood. regards.



    ________________________________
    From: Stuart Cooper <stuart.cooper@gmail.com>
    To: Rajeev Prasad <rp.neuli@yahoo.com>
    Cc: "dbi-users@perl.org" <dbi-users@perl.org>
    Sent: Friday, July 29, 2011 5:21 PM
    Subject: Re: question on CPAN example for DBI

    Hi Rajeev,

    $baz is a variable that gets substituted for the ? in the prepare
    statement, between the $dbh->prepare() and the $sth->execute.

    So it's really:

    $sth = $dbh->prepare("SELECT foo, bar FROM table WHERE baz=?");

    ... extra lines here... calculating $baz

    $sth->execute( $baz );

    Read a bit further down in perldoc DBI and you should hit a paragraph saying
    The "?" characters are placeholders...

    You don't have to use ? placeholders if you know well in advance what
    you're looking for
    and you don't want to run that same query multiple times with
    different baz values.

    $sth = $dbh->prepare("SELECT foo, bar FROM table WHERE baz=5");
    $sth->execute();

    is fine.

    Hope this helps,
    Stuart.
    On Sat, Jul 30, 2011 at 8:13 AM, Rajeev Prasad wrote:
    Hello,

    I am trying to understand this code from CPAN hepl page:



    $sth = $dbh->prepare("SELECT foo, bar FROM table WHERE baz=?"); $sth->execute( $baz ); while ( @row = $sth->fetchrow_array ) { print "@row\n"; }


    what is execute doing here with $baz? what is $baz representing?

    thank you.
    Rajeev

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupdbi-users @
categoriesperl
postedJul 29, '11 at 10:13p
activeJul 29, '11 at 10:30p
posts4
users3
websitedbi.perl.org

People

Translate

site design / logo © 2022 Grokbase