Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

bind_param in DBI not working

by neilwatson (Priest)
on Mar 15, 2014 at 18:42 UTC ( #1078475=perlquestion: print w/replies, xml ) Need Help??
neilwatson has asked for the wisdom of the Perl Monks concerning the following question:


I'm having trouble using bind_param in DBI with the Postgresql driver. I'm using the results of one query as bind params for a second query. The loop does print out $bind_class, but bind_param statement seems to do nothing. Only the bind_param outside of the loop results in something being returned. What have I done wrong?

my $sth = $dbh->prepare( $query ) || die "Could not prepare" ; foreach my $row ( @$class_arrayref ) { ( $bind_class ) = @$row; print "class = ".$bind_class."\n"; #debugging, works! $sth->bind_param( 1, $bind_class ); # does not work } my $class = 'debian%'; $sth->bind_param( 1, $class ); # works! $sth->execute; my $rows = $sth->fetchall_arrayref();


I did not understand how bind_param works. I understood that this would execute the same statement multiple times with a different where clause and I could combine the results. I now see that was silly and inefficient of me. Sorry for the noise. Feel free to reap this node.

Neil Watson

Replies are listed 'Best First'.
Re: bind_param in DBI not working
by hippo (Canon) on Mar 15, 2014 at 19:28 UTC

    I do not understand why you would repeatedly bind different values to the same parameter (number 1) within the foreach loop without calling $sth->execute. What do you expect to happen here?

    It might be more instructive if you could provide a complete, self-contained executable script which clearly demonstrates the problem you face. In this code snippet there are too many unknowns for an outside viewer to debug it as it stands. It would also be useful to know which versions you are running (of perl, of DBI, of DBD::Pg and of the postgresql server).

Re: bind_param in DBI not working
by Erez (Priest) on Mar 16, 2014 at 16:37 UTC

    Sorry for the noise. Feel free to reap this node

    One man's mistake is another's gain. At least you can rest assured someone may one day find your question and the answer and through those understand the usage of bind_param, and will be in your debt.

    Principle of Least Astonishment: Any language that doesn’t occasionally surprise the novice will pay for it by continually surprising the expert

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1078475]
Approved by davido
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2018-06-20 02:07 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.