Gossamer Forum
Home : General : Perl Programming :

How "binding" is bind_param?

Quote Reply
How "binding" is bind_param?
If I prepare a statement handle, bind parameter(s), execute, and then go into a loop, can I bind_param again within the loop prior to execution? Here's what I mean:

Code:
$sth = $dbh->prepare( "SELECT field1 FROM table WHERE primary_key_field = ? ");
$sth->bind_param(1, $var, { TYPE => SQL_INTEGER });
$sth->execute;
while ( @row = $sth->fetchrow_array ) {
$var2 = $row[0];
}
while ($ctl == 0) {
*** do stuff ***
$sth->bind_param(1, $var2, { TYPE => SQL_INTEGER });
$sth->execute;
while ( @row = $sth->fetchrow_array ) {
$var2 = $row[0];
}
*** do stuff ***
}
Hope that helps - my question is whether the bind_param that occurs inside the while ($ctl == 0) loop actually works, or will the executed statement handle always return data based on the bind_param that took place outside the loop?

The database is mySQL.
Quote Reply
Re: [SandyM] How "binding" is bind_param? In reply to
Hi,

I would think it would work, but why not just do:

$sth->execute($var)

or

$sth->execute($var2)

instead of binding it?

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] How "binding" is bind_param? In reply to
I know that the sth->execute(@bind_values) method will work in a loop, but $sth->execute($var) assumes that the value of $var is of the SQL_VARCHAR type, which is not always a desirable assumption. (This default behaviour isn't the case with all drivers, but I think it is with mySQL). bind_param allows me to specify the datatype, and I've been using it out of habit, really.

However, one of the things about bind_param is that the datatype remains the same after the first call. Although why you'd want to change it in between calls I can't imagine... but this permanence is what led me to wonder about the loop question.