Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

bind_param in DBI not working

by neilwatson (Curate)
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:

Greetings,

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();

UPDATE

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
watson-wilson.ca

Comment on bind_param in DBI not working
Download Code
Re: bind_param in DBI not working
by hippo (Curate) 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 (Curate) 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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2014-09-18 18:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (120 votes), past polls