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

I can write no problem, but cannot for the life of me figure out how to perform a select for read purposes and print the variable out.

Here is my task: Read an email address from database and send email to address

my $sth = $dbh->prepare(<<SQL); Select Email1 from client where reg = ? SQL $sth->execute($id) or err_trap; while ($sth->fetchrow) { print "string $_"; }

The problem is after $sth-> execute line. I cannot figure how to read the value to the select into a scalar. I can do this without using placeholders, but do not want to. I like to security of using placeholders.

THanks

Replies are listed 'Best First'.
Re: Returning the value of a placeholder with select
by lachoy (Parson) on Oct 17, 2002 at 20:09 UTC

    This has nothing to do with placeholders. Try one of the following:

    $sth->execute( $id ) or err_trap; while ( my $row = $sth->fetchrow_arrayref ) { print "Email is: $row->[0]\n"; } $sth->execute( $id ) or err_trap; while ( my ( $email ) = $sth->fetchrow_array ) { print "Email is: $email\n"; } $sth->execute( $id ) or err_trap; my ( $email ); $sth->bind_columns( \$email ); while ( $sth->fetch ) { print "Email is: $email\n"; }

    Chris
    M-x auto-bs-mode

Re: Returning the value of a placeholder with select
by runrig (Abbot) on Oct 17, 2002 at 20:07 UTC
    One way is to use bind_col or bind_columns:
    $sth->execute($id) or err_trap; $sth->bind_col(1, \my $e_mail); while ($sth->fetch) { print "$e_mail\n"; }
    Or without bind_col:
    while (my ($e_mail) = $sth->fetchrow_array) { ...
    Also, I notice you're checking for errors on the execute, but not on the prepare. That's a sign that you may be missing something, and maybe should consider using RaiseError (see the DBI docs) and maybe HandleError also.